package com.zfire.mall.manager.logic.front;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.front.*;
import com.zfire.mall.manager.commonmapper.front.CustomerFrontMapper;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.enums.JiaXianProcessStatusEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.CostMerchant;
import com.zfire.mall.manager.plus.entity.CostSales;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.plus.service.CostMerchantService;
import com.zfire.mall.manager.plus.service.CostSalesService;
import com.zfire.mall.manager.plus.service.KingDeeCustomerService;
import com.zfire.mall.manager.utils.JiaXianUtils;
import com.zfire.mall.manager.utils.RedisUtil;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author changelin
 */
@Service
@Slf4j
public class CustomerCostBillOrderLogic {

    @Autowired
    CustomerFrontMapper customerFrontMapper;

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    KingDeeCustomerService kingDeeCustomerService;

    @Autowired
    JiaXianUtils jiaXianUtils;

    @Autowired
    CostMerchantService costMerchantService;

    @Autowired
    CostSalesService costSalesService;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 商家物流费用账单
     * @param page
     * @param zfireParam
     * @return
     */
    public IPage<CostBillListVO> list(Page page, ZfireParamBean zfireParam) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerNumber = adminUser.getIsCustomer() ? adminUser.getCustomerName() : "";
        zfireParam = FieldUtils.supplyParam(zfireParam);
        IPage<CostBillListVO> costBillListVOIPage = customerFrontMapper.listCostBill(page, zfireParam,customerNumber,null);
        return costBillListVOIPage;
    }

    public IPage<CostBillListVO> listExportV2(Page page, ZfireParamBean zfireParam) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerNumber = adminUser.getIsCustomer() ? adminUser.getCustomerName() : "";
        if (StringUtils.isEmpty(customerNumber)) {
            IPage<CostBillListVO> costBillListVOIPage = new Page<>();
             return costBillListVOIPage;
        }

        List<KingDeeCustomer> list = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getHigherPrantNumber, customerNumber).select(KingDeeCustomer::getName).list();
        List<String> customerNumberS=new ArrayList<>();
        for (KingDeeCustomer kingDeeCustomer : list) {
            customerNumberS.add(kingDeeCustomer.getName());
        }
        zfireParam = FieldUtils.supplyParam(zfireParam);
        IPage<CostBillListVO> costBillListVOIPage = customerFrontMapper.listCostBill(page, zfireParam,customerNumber,customerNumberS);
        return costBillListVOIPage;
    }

    public IPage<CostMerchantListVO> listMerchant(Page page, ZfireParamBean zfireParam) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerNumber = adminUser.getIsCustomer() ? adminUser.getCustomerName() : "";
        zfireParam = FieldUtils.supplyParam(zfireParam);
        IPage<CostMerchantListVO> costBillListVOIPage = customerFrontMapper.listMerchant(page, zfireParam,customerNumber,null);
        return costBillListVOIPage;
    }

    public IPage<CostSalesListVO> listSales(Page page, ZfireParamBean zfireParam) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerNumber = adminUser.getIsCustomer() ? adminUser.getCustomerName() : "";
        zfireParam = FieldUtils.supplyParam(zfireParam);
        IPage<CostSalesListVO> costBillListVOIPage = customerFrontMapper.listSales(page, zfireParam,customerNumber,null);
        return costBillListVOIPage;
    }

    @Transactional(rollbackFor = Exception.class)
    public void merchantCheck(List<String> merchantCheckCode) {
        for (String merchant : merchantCheckCode) {
            CostMerchant costMerchant = costMerchantService.lambdaQuery().eq(CostMerchant::getMerchantCheckCode, merchant).last("limit 1").one();
            if (!costMerchant.getProcessStatus().equals(Constant.JIAXIAN_COST_IN_MERCHANT_CHECK)){
                throw new RemoteServiceException("账单非商家对账状态，不能完成状态");
            }
        }
        jiaXianUtils.merchantCheck(merchantCheckCode);
        for (String merchant : merchantCheckCode) {
            costMerchantService.lambdaUpdate().eq(CostMerchant::getMerchantCheckCode, merchant)
                    .set(CostMerchant::getProcessStatus,Constant.JIAXIAN_COST_WAIT_START_SALES_CHECK)
                    .update();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void salesCheck(List<String> salesCheckCode) {
        for (String sales : salesCheckCode) {
            CostSales costSales = costSalesService.lambdaQuery().eq(CostSales::getSalesCheckCode, sales).last("limit 1").one();
            if (!costSales.getProcessStatus().equals(Constant.JIAXIAN_COST_IN_SALES_CHECK)){
                throw new RemoteServiceException("账单非销司对账状态，不能完成状态");
            }
        }
        jiaXianUtils.salesCheck(salesCheckCode);
        for (String sales : salesCheckCode) {
            costSalesService.lambdaUpdate().eq(CostSales::getSalesCheckCode, sales)
                    .set(CostSales::getProcessStatus,Constant.JIAXIAN_COST_FINISH_CHECK)
                    .update();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void ticketGoToReview(CreateTicketBill createTicketBill) {

        String ticketCode = Convert.toStr(redisUtil.get(Constant.JIAXIAN_SALES_CHECK+createTicketBill.getOrderCode()));
        if (StringUtils.isEmpty(ticketCode)) {
            CreateTicketMain createTicketMain = new CreateTicketMain();
            createTicketMain.setTicketType(createTicketBill.getTicketType());
            createTicketMain.setOrderCode(createTicketBill.getOrderCode());
            ticketCode = jiaXianUtils.createTicketMain(createTicketMain);
            redisUtil.set(Constant.JIAXIAN_SALES_CHECK + createTicketBill.getOrderCode(), ticketCode,60*60*7);
        }

        if (createTicketBill.getTicketType().equals(Constant.JIAXIAN_MERCHANT_CHECK)) {
            costMerchantService.lambdaUpdate().eq(CostMerchant::getMerchantCheckCode, createTicketBill.getOrderCode())
                    .set(CostMerchant::getTicketCode,ticketCode)
                    .set(CostMerchant::getProcessStatus,JiaXianProcessStatusEnum.COST_CAN_START_MERCHANT_CHECK)
                    .update();
        }
        if (createTicketBill.getTicketType().equals(Constant.JIAXIAN_SALES_CHECK)){
            costSalesService.lambdaUpdate().eq(CostSales::getSalesCheckCode, createTicketBill.getOrderCode())
                    .set(CostSales::getTicketCode,ticketCode)
                    .update();
        }
        for (CreateTicketBillList createTicketBillList : createTicketBill.getModifiedCostBillList()) {
            createTicketBillList.setTicketCode(ticketCode);
        }
        createTicketBill.setTicketCode(ticketCode);
        jiaXianUtils.updateTicket(createTicketBill);
        List<String> review = new ArrayList<>();
        review.add(ticketCode);
        jiaXianUtils.ticketGoToReview(review);
        redisUtil.del(Constant.JIAXIAN_SALES_CHECK+createTicketBill.getOrderCode());

    }

    public IPage<CostTicketListVO> listTicket(Page page, ZfireParamBean zfireParam) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerNumber = adminUser.getIsCustomer() ? adminUser.getCustomerName() : "";
        zfireParam = FieldUtils.supplyParam(zfireParam);
        IPage<CostTicketListVO> costBillListVOIPage = customerFrontMapper.listTicket(page, zfireParam,customerNumber,null);
        return costBillListVOIPage;
    }
}
