package com.jeequan.jeepay.agent.ctrl.merchant;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.db.entity.PackageInfo;
import com.jeequan.jeepay.db.entity.PackageOrder;
import com.jeequan.jeepay.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/packageInfo")
public class PackageInfoController extends CommonCtrl {

    @Autowired private PackageInfoService packageInfoService;
    @Autowired private PackageProductService packageProductService;
    @Autowired private ProductInfoService productInfoService;
    @Autowired private ProductAppService productAppService;
    @Autowired private PackageTypeService packageTypeService;

    @RequestMapping(value = "/getPackageList", method = RequestMethod.GET)
    @MethodLog(remark = "方案中心列表")
    public ApiRes getPackageList() throws BizException {
        PackageInfo packageInfo = getObject(PackageInfo.class);
        Page<PackageInfo> page = new Page<>();
        List<PackageInfo> packageInfoList = packageInfoService.getPackageList(packageInfo.getName(),packageInfo.getState(),packageInfo.getPackageType());
        page.setRecords(packageInfoList);
        page.setTotal(packageInfoList.size());
        page.setCurrent(1);
        return ApiRes.ok(page);
    }

    @RequestMapping(value = "/getPackageById", method = RequestMethod.GET)
    @MethodLog(remark = "商户端方案详情")
    public ApiRes getPackageById() throws BizException {
        //获取查询条件
        PackageInfo packageInfo = getObject(PackageInfo.class);
        PackageInfo info  = packageInfoService.getPackageById(packageInfo.getPackageId());
        return ApiRes.ok(info);
    }


    @GetMapping("/getPackageOder")
    @MethodLog(remark = "开通记录")
    public ApiRes getPackageOder(){
        Page<PackageOrder> page=getIPage(true);
        PackageOrder packageOrder = getObject(PackageOrder.class);
        // 时间搜索
        Date[] searchDateRange = packageOrder.buildQueryDateRange();
        packageOrder.setFirstDate(searchDateRange[0]);
        packageOrder.setLastDate(searchDateRange[1]);
        //packageOrder.setCreatedUid(getCurrentUser().getSysUserId());
        //查询该服务商下的所有用户数据（包括自己和下级服务商）
        String currentAgentNo = getCurrentAgentNo();
        List<String> subAgentNoList = agentInfoService.queryAllSubAgentNo(currentAgentNo);
        List<String> mchInfoList = mchInfoService.list(MchInfo.gw()
                        .in(MchInfo::getAgentNo, subAgentNoList)
                        .eq(MchInfo::getState,1))
                .stream()
                .map(MchInfo::getMchNo).collect(Collectors.toList());
        packageOrder.setMchInfoList(mchInfoList);
        Page<PackageOrder> result = productAppService.getProductAppList(page, packageOrder);
        return ApiRes.ok(result);
    }

    @RequestMapping(value = "/getPackageOderById", method = RequestMethod.GET)
    @MethodLog(remark = "开通记录详情")
    public ApiRes getPackageOderById() throws BizException {
        //获取查询条件
        PackageOrder packageOrder = getObject(PackageOrder.class);
        PackageOrder serviceById = productAppService.getDetail(packageOrder.getId());
        return ApiRes.ok(serviceById);
    }

    @MethodLog(remark = "更新开通记录")
    @RequestMapping(value="/{id}", method = RequestMethod.PUT)
    public ApiRes update(@PathVariable("id") String id) {
        PackageOrder packageOrder = getObject(PackageOrder.class);
        packageOrder.setId(id);//设置主键
        packageOrder.setUpdatedAt(new Date());
        packageOrder.setState(1);
        productAppService.updateById(packageOrder);
        return ApiRes.ok();
    }

    @RequestMapping(value = "/getPackageTypeList", method = RequestMethod.GET)
    @MethodLog(remark = "获取所有方案分类列表")
    public ApiRes getPackageTypeList() throws BizException {
        return ApiRes.ok(packageTypeService.list());
    }

}
