package com.bonus.sgzb.app.service.impl;

import com.bonus.sgzb.app.domain.*;
import com.bonus.sgzb.app.mapper.PurchaseInputMapper;
import com.bonus.sgzb.app.service.PurchaseInputService;
import com.bonus.sgzb.base.api.domain.MaInputRecord;
import com.bonus.sgzb.base.api.domain.MaInputVO;
import com.bonus.sgzb.base.api.domain.MaMachine;
import com.bonus.sgzb.base.api.domain.MaType;
import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * @author bns
 */
@Service
public class PurchaseInputServiceImpl implements PurchaseInputService {

    @Resource
    private PurchaseInputMapper purchaseInputMapper;


    @Override
    public List<PurchaseCheckInfo> getList(PurchaseCheckInfo bean) {
        List<PurchaseCheckInfo> purchaseCheckInfos = purchaseInputMapper.selectPutInListList(bean);
        for (PurchaseCheckInfo checkInfo : purchaseCheckInfos) {
            String typeName = purchaseInputMapper.selectTypeNameByTaskId(checkInfo.getTaskId(),"2");
            checkInfo.setPurchasingTypeName(typeName);
        }
        return purchaseCheckInfos;
    }

    @Override
    public List<PurchaseMacodeInfo> selectPutinDetails(PurchaseMacodeInfo purchaseMacodeInfo) {
        return purchaseInputMapper.selectPutinDetails(purchaseMacodeInfo);
    }

    @Override
    public int modifyManageStatus(MaInputVO maInputVO) {
        Long taskId = maInputVO.getTaskId();
        String checkResult = maInputVO.getCheckResult();
        List<MaInputRecord> inputRecordList = maInputVO.getInputRecordList();
        for (MaInputRecord maInputRecord : inputRecordList) {
            Long typeId = maInputRecord.getTypeId();
            // 修改机具入库状态
            MaMachine maMachine = new MaMachine();
            maMachine.setMaId(maInputRecord.getMaId());
            maMachine.setMaCode(maInputRecord.getMaCode());

            //修改编码管理表入库状态
            PurchaseMacodeInfo purchaseMacodeInfo = new PurchaseMacodeInfo();
            purchaseMacodeInfo.setTaskId(taskId);
            purchaseMacodeInfo.setTypeId(typeId);
            purchaseMacodeInfo.setMaCode(maInputRecord.getMaCode());

            // 修改验收任务详细表入库数量
            if ("1".equals(checkResult)) {
                //修改机具状态为在库
                maMachine.setMaStatus("15");
                purchaseInputMapper.updateMaMachine(maMachine);


                // 修改编码管理的入库状态
                MaType maType = purchaseInputMapper.selectTypeByTypeId(typeId);
                // 库存添加
                maType.setNum((maInputRecord.getInputNum() == null ? 0 : maInputRecord.getInputNum())
                        + (maType.getNum() == null ? 0 : maType.getNum()));
                purchaseInputMapper.updateTypeByTypeId(maType);

                Integer inputNum = purchaseInputMapper.selectInputNumByTypeId(taskId, typeId);
                PurchaseCheckDetails purchaseCheckDetails = new PurchaseCheckDetails();
                purchaseCheckDetails.setTaskId(taskId);
                purchaseCheckDetails.setTypeId(typeId);
                purchaseCheckDetails.setInputNum(inputNum + (maInputRecord.getInputNum() == null ? 0 : maInputRecord.getInputNum()));
                purchaseCheckDetails.setStatus(4);
                purchaseInputMapper.updateByTaskIdTypeId(purchaseCheckDetails);

                // 新增入库任务详细表
                InputApplyDetails applyDetails = new InputApplyDetails();
                applyDetails.setTaskId(taskId);
                applyDetails.setTypeId(typeId);
                applyDetails.setInputNum(maInputRecord.getInputNum() == null ? 0 : maInputRecord.getInputNum());
                applyDetails.setMaId(maInputRecord.getMaId());
                applyDetails.setInputType("1");
                applyDetails.setCreateTime(new Date());
                applyDetails.setCreateBy(SecurityUtils.getUsername());
                applyDetails.setCompanyId(maInputRecord.getCompanyId());
                purchaseInputMapper.insertInputApplyDetails(applyDetails);

                purchaseMacodeInfo.setStatus("1");
                purchaseInputMapper.updateMacodeByType(purchaseMacodeInfo);

                // 新增入库记录
                purchaseInputMapper.insertMaInputRecord(maInputRecord);
            } else {
                //删除ma_machine表
                purchaseInputMapper.deleteMaMachineInfoByMaId(maInputRecord.getMaId());
                //删除ma_machine_label表
                purchaseInputMapper.deleteMaMachineLabelByMaId(maInputRecord.getMaId());
                //删除ma_label_bind表
                purchaseInputMapper.deleteMaLabelBindByMaId(maInputRecord.getMaId());
                //修改purchase_macode_info表
                purchaseInputMapper.updatetePurchaseMaCodeInfoByMaCodeAndTaskIdAndTypeId(taskId, typeId, maInputRecord.getMaCode());
                //修改purchase_check_details表
                purchaseInputMapper.updatePurchaseCheckDetailsByTaskId(taskId, typeId);
            }
        }
        //判断是否全部已操作（通过或不通过）
        int count=purchaseInputMapper.isOperateAll(taskId);
        if (count<=0){
            //全部审核任务状态改为已审核
            TmTask task = new TmTask();
            task.setTaskId(taskId);
            task.setTaskStatus(28);
            task.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUserid()));
            task.setUpdateTime(DateUtils.getNowDate());
            purchaseInputMapper.updateTmTask(task);
        }
        return 1;
    }

    @Override
    public int selectExamineType(Long companyId) {
        return purchaseInputMapper.selectExamineType(companyId);
    }

    @Override
    public List<PurchaseCheckInfo> selectPutInListExamine(PurchaseCheckInfo bean) {
        List<PurchaseCheckInfo> purchaseCheckInfos = purchaseInputMapper.selectPutInListExamine(bean);
        for (PurchaseCheckInfo checkInfo : purchaseCheckInfos) {
            String typeName = purchaseInputMapper.selectTypeNameByTaskId(checkInfo.getTaskId(),"3");
            checkInfo.setPurchasingTypeName(typeName);
        }
        return purchaseCheckInfos;
    }
}
