package com.pj.api.h5;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.pj.api.bo.InOutRecordBO;
import com.pj.api.bo.OtherBusinessBO;
import com.pj.api.service.ApiService;
import com.pj.constants.RoleEnum;
import com.pj.constants.UserTypeEnum;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project.tb_business.CarDisincle;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project.tb_business_car.TbBusinessCar;
import com.pj.project.tb_business_car.TbBusinessCarService;
import com.pj.project.tb_business_item.TbBusinessItem;
import com.pj.project.tb_costomer.TbCostomer;
import com.pj.project.tb_costomer.TbCostomerService;
import com.pj.project.tb_declare.TbDeclare;
import com.pj.project.tb_disinfect.TbDisinfect;
import com.pj.project.tb_district.TbDistrict;
import com.pj.project.tb_item.TbItem;
import com.pj.project4sp.admin4login.SpAccAdminService;
import com.pj.project4sp.global.BusinessException;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.sg.NbUtil;
import com.pj.utils.so.SoMap;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RequestMapping(value = "/api")
@RestController
@Slf4j
public class ApiController {

    @Resource
    ApiService apiService;
    @Resource
    SpAccAdminService spAccAdminService;
    @Resource
    TbCostomerService tbCostomerService;


    /**
     * 账号、密码登录
     */
    @RequestMapping("doLogin")
    AjaxJson doLogin(String key, String password, String openid) {
        // 1、验证参数
        if (NbUtil.isOneNull(key, password)) {
            return AjaxJson.getError("请提供key与password参数");
        }
        return spAccAdminService.doLogin(key, password, openid);
    }

    @RequestMapping("doLoginByOpenid")
    AjaxJson doLoginByOpenid(String openid) {
        return spAccAdminService.doLoginByOpenid(openid);
    }

    @PostMapping(value = "register")
    public AjaxJson register(TbCostomer costomer) {
        tbCostomerService.register(costomer);
        return AjaxJson.getSuccess();
    }

    @RequestMapping(value = "getInOutRecord")
    public AjaxJson getInOutRecord() {
        SoMap so = SoMap.getRequestSoMap();
        String currentCustomerId = StpUserUtil.getCustomerId();
        if (!currentCustomerId.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("customerId", currentCustomerId);
        }
        List<InOutRecordBO> recordList = apiService.getInOutRecord(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), recordList);
    }

    @RequestMapping(value = "getCustomerList")
    public AjaxJson getCustomerList() {
        SoMap so = SoMap.getRequestSoMap();
        if (StpUserUtil.isLogin()) {
            String currentCustomerId = StpUserUtil.getCustomerId();
            if (!currentCustomerId.equals(UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
                so.put("id", currentCustomerId);
            }
        }
        List<TbCostomer> list = apiService.getCustomerList(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @PostMapping(value = "confirmCustomer")
    public AjaxJson confirmCustomer(Long customerId, String judgeContent) {
        apiService.confirmCustomer(customerId, judgeContent);
        return AjaxJson.getSuccess();
    }

    @PostMapping(value = "confirm")
    public AjaxJson confirmPay() {
        SoMap so = SoMap.getRequestSoMap();
        String id = so.getString("id");
        apiService.confirm(Collections.singletonList(id));
        return AjaxJson.getSuccess();
    }

    @GetMapping(value = "searchPartCar")
    public AjaxJson searchPartCar(String carNo) {
        carNo = carNo.toUpperCase();
        return AjaxJson.getSuccessData(apiService.searchPartCar(carNo));
    }

    @GetMapping(value = "getBusinessMoney")
    public AjaxJson getBusinessMoney(String carId, @RequestParam(required = false) String state) {
        return AjaxJson.getSuccessData(apiService.getBusinessMoney(carId, state));
    }

    @GetMapping(value = "getPartCarByChannel")
    public AjaxJson getPartCarByChannel(String channel) {
        StaticLog.error("查询通道车辆：{}", channel);
        return AjaxJson.getSuccessData(apiService.getPartCarByChannel(channel));
    }

    @GetMapping(value = "getDeclareList")
    public AjaxJson getDeclareList() {
        SoMap so = SoMap.getRequestSoMap();
        String customerId = StpUserUtil.getCustomerId();
        if (!StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            so.put("customerId", customerId);
        }
        return AjaxJson.getSuccessData(apiService.getDeclareList(so.startPage()));
    }

    @GetMapping(value = "getAccountInfo")
    public AjaxJson getAccountInfo() {
        return AjaxJson.getSuccessData(apiService.getAccountInfo(StpUserUtil.getCustomerId()));
    }

    @GetMapping(value = "getDiscountInfo")
    public AjaxJson getDiscountInfo() {
        SoMap so = SoMap.getRequestSoMap();
        so.put("status", 1);
        so.put("sortType", 5);
        return AjaxJson.getSuccessData(apiService.getDiscountInfo(so));
    }

    @PostMapping(value = "addDeclare")
    public AjaxJson addDeclare(@RequestBody TbDeclare tbDeclare) {
        apiService.addDeclare(tbDeclare);
        return AjaxJson.getSuccess();
    }

    @PostMapping(value = "addDisinfect")
    public AjaxJson addDisinfect(@RequestBody TbDisinfect tbDisinfect) {
        apiService.addDisinfect(tbDisinfect);
        return AjaxJson.getSuccess();
    }

    @GetMapping("getBusinessItem")
    public AjaxJson getBusinessItem() {
        SoMap so = SoMap.getRequestSoMap();
        return AjaxJson.getSuccessData(apiService.getBusinessItem(so.getString("id")));
    }

    @PostMapping(value = "pickBusinessItem")
    public AjaxJson pickBusinessItem() {
        SoMap so = SoMap.getRequestSoMap();
        return apiService.pickBusinessItem(so.getString("openid"), so.getLong("id"));
    }

    @PostMapping(value = "confirmBusinessItem")
    public AjaxJson confirmBusinessItem() {
        SoMap so = SoMap.getRequestSoMap();
        apiService.confirmBusinessItem(so.getLong("id"));
        return AjaxJson.getSuccess();
    }

    @GetMapping("getPartnerBusinessItem")
    public AjaxJson getPartnerBusinessItem() {
        SoMap so = SoMap.getRequestSoMap();
        String customerId = StpUserUtil.getCustomerId();
        if (StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            return AjaxJson.getPageData(0L, Collections.emptyList());
        }
        so.put("pickCustomerId", StpUserUtil.getCustomerId());
        so.put("sortType", 9);
        List<TbBusinessItem> list = apiService.getPartnerBusinessItem(so.startPage());
        return AjaxJson.getPageData(so.getDataCount(), list);
    }

    @GetMapping("checkUserType")
    public AjaxJson checkUserType() {
        String customerId = StpUserUtil.getCustomerId();
        int type = 1;
        if (!StrUtil.equals(customerId, UserTypeEnum.PLATFORM_ADMIN.getCustomerId())) {
            TbCostomer tbCostomer = tbCostomerService.getById(customerId);
            if (TbCostomer.CustomerEnum.BUSINESS_TYPE.getType().equals(tbCostomer.getType())) {
                type = 2;
            } else {
                type = 3;
            }
        }
        return AjaxJson.getSuccessData(type);
    }

    @RequestMapping(value = "addCarDisinfect")
    public AjaxJson addCarDisinfect(@RequestBody OtherBusinessBO otherBusinessBO) {
        apiService.addCarDisinfect(otherBusinessBO);
        return AjaxJson.getSuccess();
    }

    @RequestMapping(value = "editCarDisinfect")
    public AjaxJson editCarDisinfect(@RequestBody OtherBusinessBO otherBusinessBO) {
        apiService.editOtherBusiness(otherBusinessBO);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("addOtherBusiness")
    public AjaxJson addOtherBusiness(OtherBusinessBO otherBusinessBO) {
        otherBusinessBO.setItems(JSONUtil.toList(otherBusinessBO.getItemJson(), TbItem.class));
        apiService.addCarDisinfect(otherBusinessBO);
        return AjaxJson.getSuccess();
    }

    @RequestMapping("editOtherBusiness")
    public AjaxJson editOtherBusiness(OtherBusinessBO otherBusinessBO) {
        otherBusinessBO.setItems(JSONUtil.toList(otherBusinessBO.getItemJson(), TbItem.class));
        apiService.editOtherBusiness(otherBusinessBO);
        return AjaxJson.getSuccess();
    }


}
