<?php

namespace app\repository;

use app\model\CouponBill;
use app\model\Deduction;
use app\model\Member as MemberModel;
use app\model\Recharge;
use app\service\Repository;
use think\Collection;
use think\Model;

/**
 * 流水相关
 *
 * Class BusinessRepository
 * @package app\repository
 * @method self getInstance(Model $model = null) static
 */
class BillRepository extends Repository

{
    /**
     * 流水列表
     * @param $page
     * @param $size
     * @param $keyword
     * @param null $businessCode
     * @param null $startTime
     * @param null $endTime
     * @param array $orders
     * @return array
     */
    public function billList($page, $size, $keyword = null, $businessCode = null,$startTime = null, $endTime = null, $orders = ["a.id" => "desc"])
    {
        $failData = [
            'total' => 0,
            'current' => $page,
            'size' => $size,
            'list' => new Collection(),
        ];
        $rep = CouponBill::alias("a")
            ->join("business b", "a.business_code = b.code")
            ->join("account c", "a.user_code = c.user_code")
            ->join("coupon_main d", "a.coupon_main_id = d.id")
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->where("b.business_name|c.nick_name|d.name", "like", "%$keyword%");
            })
            ->when(!empty($businessCode), function ($q) use ($businessCode) {
                $q->where("b.code", "=", $businessCode);
            })
            ->when(MemberModel::is_agency(session("auth")['roles']), function ($q) {
                $q->where("b.agency_code", "=", session("auth")['business_code']);
            })
            ->when(!empty($startTime), function ($q) use ($startTime) {
                $q->whereTime("a.create_time", ">=", $startTime);
            })
            ->when(!empty($endTime), function ($q) use ($endTime) {
                $q->whereTime("a.create_time", "<=", $endTime);
            })
            ->field("a.*,b.business_name,c.nick_name,c.avatar_url,d.name as coupon_main_name");

        $failData ['total'] = $rep->count();
        $failData ['list'] = $rep
            ->page($page, $size)
            ->order($orders)
            ->select();
        return $failData;
    }


    /**
     * api获取签到流水列表
     * @param $page
     * @param $size
     * @param $keyword
     * @param null $businessCode
     * @param null $startTime
     * @param null $endTime
     * @param array $orders
     * @return array
     */
    public function apiBillList($page, $size, $businessCode = null,$startTime = null, $endTime = null, $orders = ["id" => "desc"])
    {

        return CouponBill::with(["couponMain","account"])
            ->when(!empty($businessCode), function ($q) use ($businessCode) {
                $q->where("business_code", "=", $businessCode);
            })
            ->when(!empty($startTime), function ($q) use ($startTime) {
                $q->whereTime("create_time", ">=", $startTime);
            })
            ->when(!empty($endTime), function ($q) use ($endTime) {
                $q->whereTime("create_time", "<=", $endTime);
            })
            ->page($page,$size)
            ->order($orders)
            ->select();
    }



    /**
     * 充值流水列表
     * @param $page
     * @param $size
     * @param $keyword
     * @param null $startTime
     * @param null $endTime
     * @param array $orders
     * @return array
     */
    public function rechargeBillList($page, $size, $keyword = null, $startTime = null, $endTime = null, $orders = ["a.id" => "desc"])
    {

        $failData = [
            'total' => 0,
            'current' => $page,
            'size' => $size,
            'list' => new Collection(),
        ];
        $rep = Recharge::alias("a")
            ->join("account b", "a.open_id = b.open_id")
            ->join("business c", "a.business_code = c.code")
            ->join("member d", "a.member_id = d.id", "left")
            ->join("business e", "c.agency_code = e.code", "left")
            ->when(!empty($startTime), function ($q) use ($startTime) {
                $q->whereTime("a.update_time", ">=", $startTime);
            })
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->where("c.business_name|e.business_name", "like", "%$keyword%");
            })
            ->when(!empty($endTime), function ($q) use ($endTime) {
                $q->whereTime("a.update_time", "<=", $endTime);
            })
            ->field(
                [
                    "a.*",
                    "b.nick_name",
                    "b.avatar_url",
                    "d.nickname as member_nickname",
                    "c.business_name",
                    "e.business_name as agency_business_name",
                ]
            );

        $failData ['total'] = $rep->count();
        $failData ['list'] = $rep
            ->page($page, $size)
            ->order($orders)
            ->select();
        return $failData;
    }

    /**
     * 扣款流水列表
     * @param $page
     * @param $size
     * @param $keyword
     * @param null $startTime
     * @param null $endTime
     * @param array $orders
     * @return array
     */
    public function deductionBillList($page, $size, $keyword = null, $startTime = null, $endTime = null, $orders = ["id" => "desc"])
    {

        $failData = [
            'total' => 0,
            'current' => $page,
            'size' => $size,
            'list' => new Collection(),
        ];
        $rep = Deduction::alias("a")
            ->join("business c", "a.business_code = c.code")
            ->join("business f", "f.code = c.agency_code", "left")
            ->join("coupon_main e", "a.coupon_main_id = e.id")
            ->join("coupon_bill d", "a.bill_id = d.id")
            ->join("account b", "d.user_code = b.user_code", "left")
            ->when(!empty($startTime), function ($q) use ($startTime) {
                $q->whereTime("a.create_time", ">=", $startTime);
            })
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->where("c.business_name|f.business_name", "like", "%" . $keyword . "%");
            })
            ->when(!empty($endTime), function ($q) use ($endTime) {
                $q->whereTime("a.create_time", "<=", $endTime);
            })
            ->field(
                [
                    "a.money",
                    "a.balance",
                    "a.create_time",
                    "b.nick_name",
                    "b.avatar_url",
                    "e.name as coupon_main_name",
                    "c.business_name",
                    "f.business_name as agency_business_name",

                    "d.consumer_money",
                    "d.admin_money",
                    "d.agency_money",

                    "d.lat",
                    "d.lng",

                ]
            );

        $failData ['total'] = $rep->count();
        $failData ['list'] = $rep
            ->page($page, $size)
            ->order($orders)
            ->select();
        return $failData;
    }

    /**
     * 总收益
     * @param $field
     * @param null $keyword
     * @param null $start_time
     * @param null $end_time
     * @return float
     */
    public function getAgencyMoneySum($field, $keyword = null, $start_time = null, $end_time = null)
    {
        return CouponBill::alias("a")
            ->join("business b", "a.business_code = b.code", "left")
            ->join("business e", "a.agency_code = e.code", "left")
            ->join("account c", "a.user_code = c.user_code")
            ->join("coupon_main d", "a.coupon_main_id = d.id")
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->whereTime("c.business_name|e.business_name", "like", "%" . $keyword . "%");
            })
            ->when(!empty($start_time), function ($q) use ($start_time) {
                $q->whereTime("a.create_time", ">=", $start_time);
            })
            ->when(!empty($end_time), function ($q) use ($end_time) {
                $q->whereTime("a.create_time", "<=", $end_time);
            })
            ->when(MemberModel::is_agency(session("auth")['roles']), function ($q) {
                $q->where("a.agency_code", "=", session("auth")['business_code']);
            })
            ->sum("a." . $field);
    }

    /**
     * 总流水
     * @param $businessCode
     * @param null $startTime
     * @param null $endTime
     * @return float
     */
    public function getBillMoneySum($businessCode, $startTime=null, $endTime=null )
    {
        return CouponBill::alias("a")
            ->join("account c", "a.user_code = c.user_code")
            ->join("coupon_main b", "a.coupon_main_id = b.id")
            ->where("a.business_code",$businessCode)
            ->when(!empty($startTime), function ($q) use ($startTime) {
                $q->whereTime("a.create_time", ">=", $startTime);
            })
            ->when(!empty($endTime), function ($q) use ($endTime) {
                $q->whereTime("a.create_time", "<=", $endTime);
            })
            ->sum("b.deduction_money");
    }

    /**
     * 总充值流水
     * @param null $keyword
     * @param null $start_time
     * @param null $end_time
     * @return float
     */
    public function getRechargeMoneySum($keyword = null, $start_time = null, $end_time = null)
    {
        return Recharge::alias("a")
            ->join("account b", "a.open_id = b.open_id")
            ->join("business c", "a.business_code = c.code")
            ->join("member d", "a.member_id = d.id", "left")
            ->join("business e", "c.agency_code = e.code", "left")
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->whereTime("c.business_name|e.business_name", "like", "%" . $keyword . "%");
            })
            ->when(!empty($start_time), function ($q) use ($start_time) {
                $q->whereTime("a.update_time", ">=", $start_time);
            })
            ->when(!empty($end_time), function ($q) use ($end_time) {
                $q->whereTime("a.update_time", "<=", $end_time);
            })
            ->sum("a.money");
    }

    /**
     * 总扣款流水
     * @param null $keyword
     * @param null $start_time
     * @param null $end_time
     * @return float
     */
    public function getDeductionMoneySum($keyword = null,$start_time = null, $end_time = null)
    {
        return Deduction::alias("a")
            ->join("business c", "a.business_code = c.code")
            ->join("business f", "f.code = c.agency_code", "left")
            ->join("coupon_main e", "a.coupon_main_id = e.id")
            ->join("coupon_bill d", "a.bill_id = d.id")
            ->join("account b", "d.user_code = b.user_code", "left")
            ->when(!empty($keyword), function ($q) use ($keyword) {
                $q->where("c.business_name|f.business_name", "like", "%" . $keyword . "%");
            })
            ->when(!empty($start_time), function ($q) use ($start_time) {
                $q->whereTime("a.create_time", ">=", $start_time);
            })
            ->when(!empty($end_time), function ($q) use ($end_time) {
                $q->whereTime("a.create_time", "<=", $end_time);
            })
            ->sum("a.money");
    }

}