package com.zfire.mall.manager.logic.refund.v2;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.refund.RefundOrderBean;
import com.zfire.mall.manager.bean.refund.v2.RetailOtherRefundOrderVO;
import com.zfire.mall.manager.bean.refund.v2.RetailRefundOrderVO;
import com.zfire.mall.manager.bean.refund.v2.TradeERefundOrderVO;
import com.zfire.mall.manager.commonmapper.refund.RefundOrderReserveMapper;
import com.zfire.mall.manager.enums.OrderTypeEnum;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.EnginOrder;
import com.zfire.mall.manager.plus.entity.RefundOrder;
import com.zfire.mall.manager.plus.entity.RefundOrderItem;
import com.zfire.mall.manager.plus.entity.RetailOrder;
import com.zfire.mall.manager.plus.service.RefundOrderItemService;
import com.zfire.mall.manager.plus.service.RefundOrderService;
import com.zfire.mall.manager.zfire.bean.QueryParamBean;
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.BeanUtils;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class RefundOrderV2Logic {

    @Resource
    CommonLogic commonLogic;
    @Resource
    RefundOrderReserveMapper refundOrderReserveMapper;
    @Resource
    RefundOrderService refundOrderService;
    @Resource
    RefundOrderItemService refundOrderItemService;

    /**
     * 退订单列表
     * @param zfireParamBean
     * @return
     */
    public IPage<RetailRefundOrderVO> v2RetailListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.refund_order_type")
                .setCompare("=")
                .setValue(OrderTypeEnum.RETAIL_POLICY.getKey()));
        FieldUtils.supplyParam(zfireParamBean);
        IPage<RetailRefundOrderVO> page = refundOrderReserveMapper.v2RetailListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<RetailOtherRefundOrderVO> v2RetailOtherListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.refund_order_type")
                .setCompare("=")
                .setValue(OrderTypeEnum.RETAIL.getKey()));
        FieldUtils.supplyParam(zfireParamBean);
        IPage<RetailOtherRefundOrderVO> page = refundOrderReserveMapper.v2RetailOtherListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public IPage<TradeERefundOrderVO> v2TradeEListPage(ZfireParamBean zfireParamBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        if (adminUser.getIsCustomer()) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.customer_id")
                    .setCompare("=")
                    .setValue(adminUser.getCustomerId()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getAdminCompanyIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.admin_company_id")
                    .setCompare("IN")
                    .setValue(adminUser.getAdminCompanyIds()));
        }
        if (CollectionUtils.isNotEmpty(adminUser.getServiceIds())) {
            zfireParamBean.getParams().add(new QueryParamBean().setParam("a.service_id")
                    .setCompare("IN")
                    .setValue(adminUser.getServiceIds()));
        }
        zfireParamBean.getParams().add(new QueryParamBean().setParam("a.refund_order_type")
                .setCompare("=")
                .setValue(OrderTypeEnum.TRADE.getKey()));
        FieldUtils.supplyParam(zfireParamBean);
        IPage<TradeERefundOrderVO> page = refundOrderReserveMapper.v2TradeEListPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);

        return page;
    }

    public void recordTradeERefundOrder(EnginOrder enginOrder, List<RefundOrderItem> refundOrderItems) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        Integer count = refundOrderService.lambdaQuery()
                .eq(RefundOrder::getRefOrderId, enginOrder.getEnginOrderNo())
                .eq(RefundOrder::getRefundOrderType, OrderTypeEnum.TRADE.getKey())
                .count();
        String refundOrderId = enginOrder.getEnginOrderNo() + "T" + (count + 1);
        RefundOrder refundOrder = new RefundOrder();
        refundOrder.setRefundOrderId(refundOrderId)
                .setRefundOrderType(OrderTypeEnum.TRADE.getKey())
                .setRefOrderDate(enginOrder.getOrderDate())
                .setRefOrderId(enginOrder.getEnginOrderNo())
                .setAdminCompanyId(enginOrder.getAdminCompanyId())
                .setAdminUserId(enginOrder.getAdminUserId())
                .setAdminWebsitId(enginOrder.getAdminWebsitId())
                .setCustomerId(enginOrder.getCustomerId())
                .setCustomerName(enginOrder.getCustomerName())
                .setCustomerNumber(enginOrder.getCustomerNumber())
                .setRefEnginRecordNo(enginOrder.getRefEnginRecordNo())
                .setRefProjectName(enginOrder.getRefProjectName())
                .setRefProjectType(enginOrder.getRefProjectType())
                .setRefFactoryNo(enginOrder.getRefFactoryNo())
                .setRefUseUnit(enginOrder.getRefUseUnit())
                .setRefLinkman(enginOrder.getRefLinkman())
                .setRefPhone(enginOrder.getRefPhone())
                .setRefTel(enginOrder.getRefTel())
                .setFileNo(enginOrder.getFileNo())
                .setTakeDeposit(enginOrder.getTakeDeposit())
                .setGeLiInnerNote(enginOrder.getGeLiInerNote())
                .setGeLiNote(enginOrder.getGeLiNote())
                .setServiceId(enginOrder.getServiceId())
                .setServiceName(enginOrder.getServiceName())
                .setRefOrderRemark(enginOrder.getRemark())
                .setCreateBy(enginOrder.getCreateBy())
                .setCreateName(enginOrder.getCreateName())
                .setCreateTime(enginOrder.getCreateTime())
                .setUpdateBy(enginOrder.getUpdateBy())
                .setUpdateName(enginOrder.getUpdateName())
                .setUpdateTime(enginOrder.getUpdateTime())
                .setExamineBy(enginOrder.getConfirmBy())
                .setExamineName(enginOrder.getConfirmName())
                .setExamineTime(enginOrder.getConfirmTime())
                .setExamineRemark(enginOrder.getExamineNote())
                .setRefundBy(adminUser.getUserName())
                .setRefundName(adminUser.getUserName())
                .setRefundTime(DateUtil.date())
                .insert();

        for (RefundOrderItem item : refundOrderItems) {
            item.setRefundOrderItemId(IdWorker.getIdStr())
                    .setRefundOrderId(refundOrderId)
                    .setRefOrderId(enginOrder.getEnginOrderNo())
                    .setRefEnginRecordNo(enginOrder.getRefEnginRecordNo());
        }
        refundOrderItemService.saveBatch(refundOrderItems);
    }

    public void recordRetailRefundOrder(RetailOrder retailOrder, List<RefundOrderItem> refundOrderItems) {
        AdminUserCom adminUser = commonLogic.getAdminUser(true);
        Integer count = refundOrderService.lambdaQuery()
                .eq(RefundOrder::getRefOrderId, retailOrder.getId())
                .eq(RefundOrder::getRefundOrderType, retailOrder.getType() == 1 ? OrderTypeEnum.RETAIL.getKey() : OrderTypeEnum.RETAIL_POLICY.getKey())
                .count();
        String refundOrderId = retailOrder.getId() + "T" + (count + 1);
        new RefundOrder().setRefundOrderId(refundOrderId)
                .setRefundOrderType(retailOrder.getType() == 1 ? OrderTypeEnum.RETAIL.getKey() : OrderTypeEnum.RETAIL_POLICY.getKey())
                .setRefOrderDate(retailOrder.getTheTime())
                .setRefOrderId(retailOrder.getId())
                .setAdminCompanyId(retailOrder.getAdminCompanyId())
                .setAdminUserId(retailOrder.getAdminUserId())
                .setAdminWebsitId(retailOrder.getAdminWebsitId())
                .setCustomerId(retailOrder.getCustomerId())
                .setCustomerName(retailOrder.getCustomerName())
                .setCustomerNumber(retailOrder.getCustomerNumber())
                .setPolicyId(retailOrder.getPolicyId())
                .setPolicyCode(retailOrder.getPolicyCode())
                .setPolicyTitle(retailOrder.getPolicyTitle())
                .setPolicyRemark(retailOrder.getPolicyRemark())
                .setServiceId(retailOrder.getK3ServiceId())
                .setServiceName(retailOrder.getK3ServiceName())
                .setFileNo(retailOrder.getFileNo())
                .setCreateBy(retailOrder.getCreateBy())
                .setCreateName(retailOrder.getCreateName())
                .setCreateTime(retailOrder.getCreateTime())
                .setUpdateBy(retailOrder.getUpdateBy())
                .setUpdateName(retailOrder.getUpdateName())
                .setUpdateTime(retailOrder.getUpdateTime())
                .setExamineBy(retailOrder.getExamineBy())
                .setExamineName(retailOrder.getExamineName())
                .setExamineTime(retailOrder.getExamineTime())
                .setExamineRemark(retailOrder.getExamineRemark())
                .setRefundBy(adminUser.getUserName())
                .setRefundName(adminUser.getUserName())
                .setRefundTime(DateUtil.date())
                .insert();

        for (RefundOrderItem item : refundOrderItems) {
            item.setRefundOrderItemId(IdWorker.getIdStr())
                    .setRefundOrderId(refundOrderId)
                    .setRefOrderId(retailOrder.getId());
        }
        refundOrderItemService.saveBatch(refundOrderItems);
    }

    public RefundOrderBean retailDetail(String refundOrderId) {
        RefundOrder order = refundOrderService.getById(refundOrderId);
        RefundOrderBean bean = new RefundOrderBean();
        BeanUtils.copyProperties(order, bean);
        List<RefundOrderItem> list = refundOrderItemService.lambdaQuery()
                .eq(RefundOrderItem::getRefundOrderId, refundOrderId)
                .list();
        bean.setItems(list);
        return bean;
    }
}
