package com.fjhx.purchase.flow;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.common.enums.CodingRuleEnum;
import com.fjhx.common.service.coding.CodingRuleService;
import com.fjhx.flow.core.FlowDelegate;
import com.fjhx.purchase.entity.sales.enums.SalesReturnDetailStatusEnum;
import com.fjhx.purchase.entity.sales.enums.SalesReturnStatusEnum;
import com.fjhx.purchase.entity.sales.po.SalesReturn;
import com.fjhx.purchase.entity.sales.po.SalesReturnDetail;
import com.fjhx.purchase.service.sales.SalesReturnDetailService;
import com.fjhx.purchase.service.sales.SalesReturnService;
import com.fjhx.purchase.util.code.CodeEnum;
import com.fjhx.wms.entity.stock.emums.StockWaitType;
import com.fjhx.wms.entity.stock.po.StockWait;
import com.fjhx.wms.entity.stock.po.StockWaitDetails;
import com.fjhx.wms.service.stock.StockWaitDetailsService;
import com.fjhx.wms.service.stock.StockWaitService;
import com.obs.services.internal.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 退货流程
 *
 * @Author:caozj
 * @DATE:2023/4/3 17:38
 */
@DS(SourceConstant.PURCHASE)
@Component
public class SalesReturnFlow extends FlowDelegate {

    @Autowired
    StockWaitDetailsService stockWaitDetailsService;

    @Autowired
    private CodingRuleService codingRuleService;

    @Override
    public String getFlowKey() {
        return "sales_return_flow";
    }


    /**
     * 发起流程
     *
     * @param flowId     流程ID
     * @param submitData 数据
     * @return
     */
    @Override
    public Long start(Long flowId, JSONObject submitData) {
        SalesReturnService salesReturnService = SpringUtil.getBean(SalesReturnService.class);
        SalesReturnDetailService salesReturnDetailService = SpringUtil.getBean(SalesReturnDetailService.class);
        SalesReturn salesReturn = submitData.toJavaObject(SalesReturn.class);
//        salesReturn.setCode(CodeEnum.SALES_RETURN.getCode());
        salesReturn.setCode(codingRuleService.createCode(CodingRuleEnum.SALES_RETURN.getKey(),null));
        salesReturnService.save(salesReturn);
        List<SalesReturnDetail> salesReturnDetailList = salesReturn.getSalesReturnDetailList();
        if (CollectionUtils.isNotEmpty(salesReturnDetailList)) {
            for (SalesReturnDetail s : salesReturnDetailList) {
                s.setSalesReturnId(salesReturn.getId());
            }
            salesReturnDetailService.saveBatch(salesReturnDetailList);
        }
        return salesReturn.getId();
    }

    /**
     * 结束流程
     *
     * @param flowId     流程ID
     * @param businessId 业务ID
     * @param submitData 数据
     */
    @Override
    public void end(Long flowId, Long businessId, JSONObject submitData) {
        SalesReturnService salesReturnService = SpringUtil.getBean(SalesReturnService.class);
        SalesReturnDetailService salesReturnDetailService = SpringUtil.getBean(SalesReturnDetailService.class);
        StockWaitService stockWaitService = SpringUtil.getBean(StockWaitService.class);
        //通过业务ID查询退货数据
        SalesReturn salesReturn = salesReturnService.getById(businessId);
        if (ObjectUtils.isEmpty(salesReturn)) {
            throw new ServiceException("退货单单不存在");
        }
        //修改退货状态为待退货
        salesReturn.setStatus(SalesReturnStatusEnum.PASS.getKey());
        salesReturn.setApprovedDate(new Date());
        salesReturnService.updateById(salesReturn);
        //修改退货明细状态为待退货
        SalesReturnDetail detail = new SalesReturnDetail();
        detail.setStatus(SalesReturnDetailStatusEnum.STATUS_20.getKey());
        salesReturnDetailService.update(detail, Wrappers.<SalesReturnDetail>query()
                .lambda().eq(SalesReturnDetail::getSalesReturnId, salesReturn.getId()));
        List<SalesReturnDetail> salesReturnDetailList = salesReturnDetailService.list(Wrappers.<SalesReturnDetail>query()
                .lambda().eq(SalesReturnDetail::getSalesReturnId, salesReturn.getId()));
        //添加一份待出库数据
        StockWait stockWait = new StockWait();
        stockWait.setBusinessId(businessId);
        stockWait.setType(2);//入库
        stockWait.setStatus(0);//待入库
        stockWait.setBusinessType(StockWaitType.BACK_OUT.getDetailType());//退货出库
        stockWaitService.save(stockWait);
        List<StockWaitDetails> stockWaitDetailsList = new ArrayList<>();
        for (SalesReturnDetail s : salesReturnDetailList) {
            StockWaitDetails stockWaitDetails = new StockWaitDetails();
            stockWaitDetails.setStockWaitId(stockWait.getId());
            stockWaitDetails.setProductId(s.getBussinessId());
            stockWaitDetails.setBusinessDetailsId(s.getId());//到货明细ID
            stockWaitDetails.setQuantity(s.getCount());
            stockWaitDetailsList.add(stockWaitDetails);
        }
        stockWaitDetailsService.saveBatch(stockWaitDetailsList);
    }

}
