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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.front.CustomerOrderPushBean;
import com.zfire.mall.manager.bean.front.CustomerPushBean;
import com.zfire.mall.manager.bean.listV2.K3CustomerVO;
import com.zfire.mall.manager.bean.listV2.StageK3CustomerVO;
import com.zfire.mall.manager.commonmapper.CommonCountMapper;
import com.zfire.mall.manager.commonmapper.listV2.CommonCMapper;
import com.zfire.mall.manager.enums.CuseromtTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.KingDeeCustomer;
import com.zfire.mall.manager.plus.service.KingDeeCustomerService;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class Stagek3CustomerLogic {

    @Autowired
    CommonCMapper commonCMapper;

    @Autowired
    KingDeeCustomerService kingDeeCustomerService;

    @Autowired
    CommonLogic commonLogic;


    public IPage<StageK3CustomerVO> listV2(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        String customerId = adminUser.getIsCustomer() ? adminUser.getCustomerNumber() : "";
        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<StageK3CustomerVO> k3CustomerVOIPage = commonCMapper.stageListV2(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean,customerId);
        return k3CustomerVOIPage;
    }

    public void add(KingDeeCustomer kingDeeCustomer, HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
    /*    if (!adminUser.getIsCustomer())
            throw new RemoteServiceException("非经销商账号不可创建二级经销商");*/
        KingDeeCustomer byId = new KingDeeCustomer();
        if (adminUser.getIsCustomer()) {
             byId = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getNumber, adminUser.getCustomerNumber()).last("limit 1").one();
        }else {
            byId = kingDeeCustomerService.lambdaQuery().eq(KingDeeCustomer::getNumber, kingDeeCustomer.getHigherPrantNumber()).last("limit 1").one();
        }

        byId.setHigherPrantNumber(byId.getNumber());
        byId.setHigherPrantName(byId.getName());



        Integer count = kingDeeCustomerService.lambdaQuery()
                .like(KingDeeCustomer::getNumber, "CUST" + byId.getNumber())
                .orderByDesc(KingDeeCustomer::getId).count();
        int num = count+1;

        byId.setNumber("CUST" + byId.getNumber()+(num>9?num:"0"+num));

        byId.setJiaxianType(kingDeeCustomer.getJiaxianType().equals(CuseromtTypeEnum.GONGCHENG.getKey())
                ?CuseromtTypeEnum.GONGCHENG.getKey():CuseromtTypeEnum.JIAXIAN.getKey());
        byId.setId(IdWorker.getIdStr());
        byId.setName(kingDeeCustomer.getName());
        byId.setChargeName(kingDeeCustomer.getChargeName());
        byId.setMobile(kingDeeCustomer.getMobile());
        byId.setInvoiceAddress(kingDeeCustomer.getInvoiceAddress());
        byId.setForbidStatus(kingDeeCustomer.getForbidStatus());
        byId.setUrl(kingDeeCustomer.getUrl());
        if (!StringUtils.isBlank(kingDeeCustomer.getAfterNumber())) {
            byId.setAfterNumber(kingDeeCustomer.getAfterNumber());
            byId.setBinding("YES");
            byId.setBindingName(adminUser.getNickName());
            byId.setBindingTime(new Date());
        }
        byId.insert();


    }

    public void update(KingDeeCustomer kingDeeCustomer, HttpServletRequest request) {

        kingDeeCustomer.setJiaxianType(kingDeeCustomer.getJiaxianType().equals(CuseromtTypeEnum.GONGCHENG.getKey())
                ?CuseromtTypeEnum.GONGCHENG.getKey():CuseromtTypeEnum.JIAXIAN.getKey());
        kingDeeCustomer.updateById();
    }

    public KingDeeCustomer detail(String id) {
        return kingDeeCustomerService.getById(id);
    }

    public void examine(String id, String examineStatus, String remark, HttpServletRequest request) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        KingDeeCustomer kingDeeCustomer = new KingDeeCustomer();
        kingDeeCustomer.setId(id);
        kingDeeCustomer.setExamineStatus(examineStatus);
        kingDeeCustomer.setApprovalRemark(remark);
        kingDeeCustomer.setApprovalName(adminUser.getNickName());
        kingDeeCustomer.updateById();
    }

    public List<CustomerPushBean> listPush(String encrypted) {
        List<CustomerPushBean> customerPushBean = commonCMapper.listPush(encrypted);
        return customerPushBean;
    }

    public IPage<K3CustomerVO> listV3(ZfireParamBean zfireParamBean) {

        zfireParamBean = FieldUtils.supplyParam(zfireParamBean);
        IPage<K3CustomerVO> k3CustomerVOIPage = commonCMapper.queryListV3K3CutomerList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return k3CustomerVOIPage;
    }
}
