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

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.gree.mall.manager.bean.material.manage.WebsitPartsOldOutRecordDTO;
import com.gree.mall.manager.bean.material.stock.WebsitStockDTO;
import com.gree.mall.manager.enums.material.DirectFlagEnum;
import com.gree.mall.manager.enums.material.PartsAttrEnum;
import com.gree.mall.manager.enums.material.PartsObjectTypeEnum;
import com.gree.mall.manager.enums.material.WebsitGoodsTypeEnum;
import com.gree.mall.manager.logic.material.stock.MaterialGoodsStockLogic;
import com.gree.mall.manager.plus.entity.WebsitPartsOldOutRecord;
import com.gree.mall.manager.plus.entity.WebsitPartsOldOutRecordBak;
import com.gree.mall.manager.plus.service.WebsitPartsOldOutRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class WebitPartsOldStockLogic {

    @Resource
    MaterialGoodsStockLogic materialGoodsStockLogic;

    public void oldOutStock(WebsitPartsOldOutRecordDTO oldOutRecord, WebsitPartsOldOutRecordService websitPartsOldOutRecordService, boolean isImport) throws Exception {
        List<WebsitStockDTO> shopStockDTOS = new ArrayList<>();

        // 入库注值
        WebsitStockDTO websitStockDTO = new WebsitStockDTO();
        websitStockDTO.setCompanyWechatId(oldOutRecord.getCompanyWechatId());
        websitStockDTO.setCompanyWechatName(oldOutRecord.getCompanyWechatName());
        websitStockDTO.setWebsitId(oldOutRecord.getSendWebsitId());
        websitStockDTO.setWebsitName(oldOutRecord.getSendWebsitName());
        websitStockDTO.setPartsWebsitId(oldOutRecord.getSendPartsWebsitId());
        websitStockDTO.setSdate(oldOutRecord.getCreateTime());
        websitStockDTO.setPartsAttr(oldOutRecord.getObjectType().equals(PartsObjectTypeEnum.NEW_REFUND.getDesc()) ? PartsAttrEnum.NEW.toString() : PartsAttrEnum.OLD.toString());
        websitStockDTO.setGoodsId(oldOutRecord.getPartsNumber());
        websitStockDTO.setGoodsName(oldOutRecord.getPartsName());
        websitStockDTO.setGoodsType(WebsitGoodsTypeEnum.P.getKey());
        websitStockDTO.setVoucherNo(isImport ? oldOutRecord.getVoucherNo() : IdWorker.getTimeId());
        websitStockDTO.setApplyNo(isImport ? oldOutRecord.getApplyNo() : IdWorker.getTimeId());
        websitStockDTO.setPrice(oldOutRecord.getPrice());
        websitStockDTO.setRef(oldOutRecord.getId());
        websitStockDTO.setRefType(oldOutRecord.getObjectType());
        websitStockDTO.setChangeQty(oldOutRecord.getQty());
        websitStockDTO.setDirectFlag(DirectFlagEnum.SUB.getKey());
        websitStockDTO.setRemark(oldOutRecord.getRemark());
        websitStockDTO.setOperateBy(isImport ? oldOutRecord.getCreateBy() : oldOutRecord.getOperateBy());
        websitStockDTO.setOperateTime(isImport ? oldOutRecord.getCreateTime() : oldOutRecord.getImportDate());
        websitStockDTO.setObj("网点");
        websitStockDTO.setStorageId(oldOutRecord.getStorageId());
        websitStockDTO.setGoodsStockUnit(oldOutRecord.getGoodsStockUnit());
        websitStockDTO.setMaterialGroupName(oldOutRecord.getMaterialGroupName());
        shopStockDTOS.add(websitStockDTO);

        // 开始处理库存
        materialGoodsStockLogic.handleWebsitStock(shopStockDTOS);

        // 库存处理完后保存记录
        if (isImport) {
            websitPartsOldOutRecordService.save(oldOutRecord);
        } else {
            websitPartsOldOutRecordService.lambdaUpdate()
                    .set(WebsitPartsOldOutRecord::getVoucherNo, websitStockDTO.getVoucherNo())
                    .set(WebsitPartsOldOutRecord::getApplyNo, websitStockDTO.getApplyNo())
                    .set(WebsitPartsOldOutRecord::getOperateBy, oldOutRecord.getOperateBy())
                    .set(WebsitPartsOldOutRecord::getImportDate, oldOutRecord.getImportDate())
                    .set(WebsitPartsOldOutRecord::getReceiveDate, oldOutRecord.getReceiveDate())
                    .eq(WebsitPartsOldOutRecord::getId, oldOutRecord.getId())
                    .update();
        }
    }

    public void delOldOutStock(WebsitPartsOldOutRecord outRecord) throws Exception {
        // 发出
        WebsitStockDTO sendShopStockDTO = new WebsitStockDTO();
        sendShopStockDTO.setCompanyWechatId(outRecord.getCompanyWechatId());
        sendShopStockDTO.setCompanyWechatName(outRecord.getCompanyWechatName());
        sendShopStockDTO.setWebsitId(outRecord.getSendWebsitId());
        sendShopStockDTO.setWebsitName(outRecord.getSendWebsitName());
        sendShopStockDTO.setSdate(outRecord.getCreateTime());
        sendShopStockDTO.setGoodsId(outRecord.getPartsNumber());
        sendShopStockDTO.setGoodsName(outRecord.getPartsName());
        sendShopStockDTO.setGoodsType(WebsitGoodsTypeEnum.P.getKey());
        sendShopStockDTO.setPrice(outRecord.getPrice());
        sendShopStockDTO.setVoucherNo(outRecord.getVoucherNo());
        sendShopStockDTO.setApplyNo(outRecord.getApplyNo());
        sendShopStockDTO.setRef(outRecord.getId());
        sendShopStockDTO.setRefType(outRecord.getObjectType());
        sendShopStockDTO.setPartsAttr(outRecord.getObjectType().equals("新件退库") ? PartsAttrEnum.NEW.getKey() : PartsAttrEnum.OLD.getKey());
        sendShopStockDTO.setChangeQty(outRecord.getQty());
        sendShopStockDTO.setDirectFlag(DirectFlagEnum.SUB.getKey());
        sendShopStockDTO.setRemark(outRecord.getRemark());
        sendShopStockDTO.setOperateBy(outRecord.getCreateBy());
        sendShopStockDTO.setOperateTime(outRecord.getCreateTime());
        sendShopStockDTO.setObj("网点");
        sendShopStockDTO.setStorageId(outRecord.getStorageId());
        sendShopStockDTO.setGoodsStockUnit(outRecord.getGoodsStockUnit());
        sendShopStockDTO.setMaterialGroupName(outRecord.getMaterialGroupName());

        materialGoodsStockLogic.handleWebsitStock(new ArrayList<WebsitStockDTO>(){{ this.add(sendShopStockDTO); }});

        // 库存处理完后删除记录及备份
        outRecord.deleteById();
        WebsitPartsOldOutRecordBak partsShopOldOutRecordBak = new WebsitPartsOldOutRecordBak();
        BeanUtils.copyProperties(outRecord, partsShopOldOutRecordBak);
        partsShopOldOutRecordBak.insert();
    }
}
