package com.zbf.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.utils.SecurityUtils;
import com.zbf.system.domain.TMiStock;
import com.zbf.system.domain.TPanDetail;
import com.zbf.system.domain.TPanOrder;
import com.zbf.system.mapper.TPanDetailMapper;
import com.zbf.system.mapper.TPanOrderMapper;
import com.zbf.system.service.ITPanOrderService;
import com.zbf.system.service.ITSeqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 盘点单Service业务层处理
 *
 * @author wgs
 * @date 2024-06-20
 */
@Service
public class TPanOrderServiceImpl extends ServiceImpl<TPanOrderMapper, TPanOrder> implements ITPanOrderService
{
    private final TPanOrderMapper tPanOrderMapper;

    @Autowired
    private TPanDetailMapper tPanDetailMapper;

    @Autowired
    private ITSeqService seqService;

    public TPanOrderServiceImpl(TPanOrderMapper tPanOrderMapper) {
        this.tPanOrderMapper = tPanOrderMapper;
    }

    /**
     * 查询盘点单列表
     *
     * @param tPanOrder 盘点单
     * @return 盘点单
     */
    public List<TPanOrder> selectTPanOrderList(TPanOrder tPanOrder)
    {
        return tPanOrderMapper.selectTPanOrderList(tPanOrder);
    }

    /**
     * 手持盘点
     */
    @Override
    public AjaxResult pdaPanConfirm(List<TMiStock> tMiStockList) {

        if (tMiStockList == null || tMiStockList.size() == 0) {
           return AjaxResult.error();
        }

        // 生成盘点单对象
        TPanOrder tPanOrder = new TPanOrder();

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); // 包含到毫秒
        tPanOrder.setPanId(seqService.generateTempSeq("PAN-" + df.format(new Date()), 4));
        tPanOrder.setId(seqService.generateTempSeq("PANID-" + df.format(new Date()), 4));

        // 仓库ID
        String storageId = tMiStockList.get(0).getStorageId();
        tPanOrder.setStorageId(storageId);
        tPanOrder.setStorageShortName(tMiStockList.get(0).getStorageShortName());

        tPanOrder.setStatus("3");
        tPanOrder.setConfirmStatus("0");
        tPanOrder.setDelFlag(0);

        tPanOrder.setCreateBy(SecurityUtils.getUserNick());
        tPanOrder.setCreateTime(new Date());

        tPanOrder.setRecordPerson(SecurityUtils.getUserNick());
        tPanOrder.setRecordDate(new Date());

        tPanOrderMapper.insertTPanOrder(tPanOrder);

        List<TPanDetail> tpDetails = new ArrayList<TPanDetail>();

        for(TMiStock tMiStock : tMiStockList) {
            TPanDetail tPanDetail = new TPanDetail();
            // 仓库ID
            tPanDetail.setStorageId(tMiStock.getStorageId());
            tPanDetail.setStorageShortName(tMiStock.getStorageShortName());
            // 物料编码
            tPanDetail.setGoodsId(tMiStock.getGoodsId());
            // 物料名称
            tPanDetail.setGoodsName(tMiStock.getGoodsName());
            // 库位
            tPanDetail.setLocationId(tMiStock.getLocationId());
            // 库存数量
            tPanDetail.setStockNum(tMiStock.getShelvesNum());
            // 盘点数量
            tPanDetail.setCheckNum(tMiStock.getRealNum());
            // 0:未清点
            tPanDetail.setStatus("1");
            // 货物状态（0：正常 1： 冻结)
            tPanDetail.setGoodsStatus("0");

            // 比较库存数量和盘点数量，设置结果字段
            // 比较库存数量和盘点数量，设置结果字段
            if (tMiStock.getRealNum() == null){
                tMiStock.setRealNum(BigDecimal.valueOf(0.00));
            }
            if (tMiStock.getRealNum().compareTo(tMiStock.getShelvesNum()) > 0) {
                tPanDetail.setRemark("盘盈");
            } else if (tMiStock.getRealNum().compareTo(tMiStock.getShelvesNum()) < 0) {
                tPanDetail.setRemark("盘亏");
            } else {
                tPanDetail.setRemark("盘平");
            }

            tPanDetail.setCreateBy(SecurityUtils.getUserNick());
            tPanDetail.setCreateTime(new Date());
            tPanDetail.setStockId(tMiStock.getId());

            tPanDetail.setRecordPerson(SecurityUtils.getUserNick());
            tPanDetail.setRecordDate(new Date());
            tPanDetail.setId(seqService.generateTempSeq("PANmx-" + df.format(new Date()), 4));
            tpDetails.add(tPanDetail);

            if (tpDetails.size() > 100) {
                tPanDetailMapper.batchInsertTPanDetail(tpDetails);
                tpDetails.clear(); // 清空列表以便下一批处理
            }
        }

        if (tpDetails.size() > 0) {
            tPanDetailMapper.batchInsertTPanDetail(tpDetails);
        }

        return AjaxResult.success();
    }
}
