package com.aucma.report.service.impl;

import java.util.HashMap;
import java.util.List;

import com.aucma.base.domain.BaseOrderInfo;
import com.aucma.base.service.IBaseOrderInfoService;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.report.mapper.RecordSwitchOrderMapper;
import com.aucma.report.domain.RecordSwitchOrder;
import com.aucma.report.service.IRecordSwitchOrderService;

import static com.aucma.common.utils.SecurityUtils.getUsername;

/**
 * 切换订单管理Service业务层处理
 *
 * @author Yinq
 * @date 2024-04-23
 */
@Service
public class RecordSwitchOrderServiceImpl implements IRecordSwitchOrderService {
    @Autowired
    private RecordSwitchOrderMapper recordSwitchOrderMapper;

    @Autowired
    private IBaseOrderInfoService baseOrderInfoService;

    /**
     * 查询切换订单管理
     *
     * @param objId 切换订单管理主键
     * @return 切换订单管理
     */
    @Override
    public RecordSwitchOrder selectRecordSwitchOrderByObjId(Long objId) {
        return recordSwitchOrderMapper.selectRecordSwitchOrderByObjId(objId);
    }

    /**
     * 查询切换订单管理列表
     *
     * @param recordSwitchOrder 切换订单管理
     * @return 切换订单管理
     */
    @Override
    public List<RecordSwitchOrder> selectRecordSwitchOrderList(RecordSwitchOrder recordSwitchOrder) {
        return recordSwitchOrderMapper.selectRecordSwitchOrderList(recordSwitchOrder);
    }

    /**
     * 新增切换订单管理
     *
     * @param recordSwitchOrder 切换订单管理
     * @return 结果
     */
    @Override
    public int insertRecordSwitchOrder(RecordSwitchOrder recordSwitchOrder) {
        try {
            recordSwitchOrder.setCreateBy(getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }
        recordSwitchOrder.setCreateTime(DateUtils.getNowDate());
        return recordSwitchOrderMapper.insertRecordSwitchOrder(recordSwitchOrder);
    }

    /**
     * 修改切换订单管理
     *
     * @param recordSwitchOrder 切换订单管理
     * @return 结果
     */
    @Override
    public int updateRecordSwitchOrder(RecordSwitchOrder recordSwitchOrder) {
        String oldOrderCode = recordSwitchOrder.getOrderCode();
        String newOrderCode = recordSwitchOrder.getOldOrderCode();

        BaseOrderInfo baseOrderInfo = new BaseOrderInfo();
        baseOrderInfo.setOrderCode(newOrderCode);
        List<BaseOrderInfo> baseOrderInfos = baseOrderInfoService.selectBaseOrderInfoList(baseOrderInfo);
        if (baseOrderInfos.size() == 0){
            throw new ServiceException("MES系统未维护此SAP订单！");
        }
        recordSwitchOrder.setIsSwitch("1");
        recordSwitchOrder.setOldOrderCode(oldOrderCode);
        recordSwitchOrder.setOrderCode(newOrderCode);
        recordSwitchOrder.setUpdateBy(getUsername());
        recordSwitchOrder.setUpdateTime(DateUtils.getNowDate());
        try {
            recordSwitchOrderMapper.updatePrintCode(recordSwitchOrder);
            recordSwitchOrderMapper.updateRecordSwitchOrder(recordSwitchOrder);
            List<HashMap<String, String>> barcodeList = recordSwitchOrderMapper.selectPrintBarcode(recordSwitchOrder.getMaterialBarcode());
            //  维护条码补打记录表
            for (HashMap<String, String> map : barcodeList) {
                map.put("print_reason", "切换订单");
                map.put("CREATE_BY", getUsername());
            }
            recordSwitchOrderMapper.printSupplementBarcode(barcodeList.get(0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    /**
     * 批量删除切换订单管理
     *
     * @param objIds 需要删除的切换订单管理主键
     * @return 结果
     */
    @Override
    public int deleteRecordSwitchOrderByObjIds(Long[] objIds) {
        return recordSwitchOrderMapper.deleteRecordSwitchOrderByObjIds(objIds);
    }

    /**
     * 删除切换订单管理信息
     *
     * @param objId 切换订单管理主键
     * @return 结果
     */
    @Override
    public int deleteRecordSwitchOrderByObjId(Long objId) {
        return recordSwitchOrderMapper.deleteRecordSwitchOrderByObjId(objId);
    }
}
