package com.gree.mall.manager.logic.material.worker;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.material.stock.WorkerStockDTO;
import com.gree.mall.manager.enums.material.*;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.logic.material.stock.MaterialGoodsStockLogic;
import com.gree.mall.manager.plus.entity.WebsitPartsOldRefundManage;
import com.gree.mall.manager.plus.entity.WebsitPartsOldRefundManageItem;
import com.gree.mall.manager.plus.service.WebsitPartsOldRefundManageItemService;
import com.gree.mall.manager.utils.CommonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class OldRefundManageInnerLogic {

    private final CommonLogic commonLogic;
    private final WebsitPartsOldRefundManageItemService websitPartsOldRefundManageItemService;
    private final MaterialGoodsStockLogic materialGoodsStockLogic;

    @Transactional(rollbackFor = Exception.class)
    public void submit(WebsitPartsOldRefundManage partsOldRefundManage) throws Exception {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (!partsOldRefundManage.getFlag().equals(PartsOrderFlagEnum.SAVE.getKey())) {
            return;
        }

        DateTime curDate = DateUtil.date();
        partsOldRefundManage
                .setSettlementState(true)
                .setImportTime(curDate)
                .setFlag(partsOldRefundManage.getRepairFlag().equals(PartsRepairFlagEnum.INNER.getKey()) ? PartsOrderFlagEnum.AGREE.getKey() : PartsOrderFlagEnum.SUBMIT.getKey())
                .setSubmitBy(adminUser.getNickName())
                .setSubmitTime(curDate)
                .updateById();

        String refType = PartsRepairFlagEnum.INNER.getKey().equals(partsOldRefundManage.getRepairFlag()) ?
                PartsRefTypeEnum.OLD_REFUND.getRemark() :
                PartsRefTypeEnum.OUTSIDE_TO_SALES.getRemark();
        // 提交状态时 旧件库存减 临占件库存加
        List<WebsitPartsOldRefundManageItem> partsOldRefundManageItems = websitPartsOldRefundManageItemService.lambdaQuery()
                .eq(WebsitPartsOldRefundManageItem::getApplyNo, partsOldRefundManage.getApplyNo())
                .list();

        List<WorkerStockDTO> workerStockDTOList = this.createWorkerStockDTO(partsOldRefundManage, partsOldRefundManageItems,
                refType, PartsAttrEnum.OLD.getKey(), PartsAttrEnum.TEMP_OLD.getKey(), PartsOrderFlagEnum.SUBMIT.getKey());
        materialGoodsStockLogic.handleWorkerStock(workerStockDTOList);

//        this.insertPartsMessageTip(partsOldRefundManage, curDate);
    }

    public List<WorkerStockDTO> createWorkerStockDTO(WebsitPartsOldRefundManage partsOldRefundManage,
            List<WebsitPartsOldRefundManageItem> partsOldRefundManageItems,
            String refType, String partsAttr1,
            String partsAttr2, String flag) {
        List<WorkerStockDTO> workerStockDTOList = new ArrayList<>();
        boolean b = flag.equals(PartsOrderFlagEnum.SAVE.getKey()) || flag.equals(PartsOrderFlagEnum.SUBMIT.getKey());
        if (StringUtils.isNotBlank(partsAttr1)) {
            for (WebsitPartsOldRefundManageItem refundManageItem : partsOldRefundManageItems) {
                WorkerStockDTO workerStockDTO = new WorkerStockDTO();
                workerStockDTO.setCompanyWechatId(partsOldRefundManage.getCompanyWechatId());
                workerStockDTO.setCompanyWechatName(partsOldRefundManage.getCompanyWechatName());
                workerStockDTO.setIdentity(partsOldRefundManage.getIdentity());
                workerStockDTO.setWorkerId(partsOldRefundManage.getWorkerId());
                workerStockDTO.setWorkerName(partsOldRefundManage.getWorkerName());
                workerStockDTO.setWebsitId(partsOldRefundManage.getReceiveWebsitId());
                workerStockDTO.setWebsitName(partsOldRefundManage.getReceiveWebsitName());
                workerStockDTO.setPartsWebsitId(partsOldRefundManage.getReceivePartsWebsitId());
                workerStockDTO.setSdate(DateUtil.date());
                workerStockDTO.setGoodsType(WebsitGoodsTypeEnum.P.getKey());
                workerStockDTO.setGoodsId(refundManageItem.getNewPartsNumber());
                workerStockDTO.setGoodsName(refundManageItem.getNewPartsName());
                workerStockDTO.setMaterialGroupName(refundManageItem.getNewMaterialGroupName());
                workerStockDTO.setGoodsStockUnit(refundManageItem.getNewUnitName());
                workerStockDTO.setPrice(BigDecimal.ZERO);
                workerStockDTO.setRef(partsOldRefundManage.getApplyNo());
                workerStockDTO.setRefType(refType);
                workerStockDTO.setPartsAttr(partsAttr1);
                workerStockDTO.setChangeQty(refundManageItem.getQty());
                workerStockDTO.setDirectFlag(DirectFlagEnum.SUB.getKey());
                workerStockDTO.setRemark(refundManageItem.getRemark());
                workerStockDTO.setOperateBy(b ? partsOldRefundManage.getWorkerName() : CommonUtils.getNickName());
                workerStockDTO.setOperateTime(DateUtil.date());

                workerStockDTOList.add(workerStockDTO);
            }
        }
        if (StringUtils.isNotBlank(partsAttr2)) {
            for (WebsitPartsOldRefundManageItem refundManageItem : partsOldRefundManageItems) {
                WorkerStockDTO workerStockDTO = new WorkerStockDTO();
                workerStockDTO.setCompanyWechatId(partsOldRefundManage.getCompanyWechatId());
                workerStockDTO.setCompanyWechatName(partsOldRefundManage.getCompanyWechatName());
                workerStockDTO.setIdentity(partsOldRefundManage.getIdentity());
                workerStockDTO.setWorkerId(partsOldRefundManage.getWorkerId());
                workerStockDTO.setWorkerName(partsOldRefundManage.getWorkerName());
                workerStockDTO.setWebsitId(partsOldRefundManage.getReceiveWebsitId());
                workerStockDTO.setWebsitName(partsOldRefundManage.getReceiveWebsitName());
                workerStockDTO.setPartsWebsitId(partsOldRefundManage.getReceivePartsWebsitId());
                workerStockDTO.setSdate(DateUtil.date());
                workerStockDTO.setGoodsType(WebsitGoodsTypeEnum.P.getKey());
                workerStockDTO.setGoodsId(refundManageItem.getNewPartsNumber());
                workerStockDTO.setGoodsName(refundManageItem.getNewPartsName());
                workerStockDTO.setMaterialGroupName(refundManageItem.getNewMaterialGroupName());
                workerStockDTO.setGoodsStockUnit(refundManageItem.getNewUnitName());
                workerStockDTO.setPrice(BigDecimal.ZERO);
                workerStockDTO.setRef(partsOldRefundManage.getApplyNo());
                workerStockDTO.setRefType(refType);
                workerStockDTO.setPartsAttr(partsAttr2);
                workerStockDTO.setChangeQty(refundManageItem.getQty());
                workerStockDTO.setDirectFlag(DirectFlagEnum.ADD.getKey());
                workerStockDTO.setRemark(refundManageItem.getRemark());
                workerStockDTO.setOperateBy(b ? partsOldRefundManage.getWorkerName() : CommonUtils.getNickName());
                workerStockDTO.setOperateTime(DateUtil.date());

                workerStockDTOList.add(workerStockDTO);
            }
        }
        return workerStockDTOList;

    }
}
