package com.bonus.material.purchase.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.material.ma.mapper.MachineMapper;
import com.bonus.material.purchase.domain.PurchaseCheckDetails;
import com.bonus.material.purchase.mapper.PurchaseCheckDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.material.purchase.mapper.PurchaseMacodeInfoMapper;
import com.bonus.material.purchase.domain.PurchaseMacodeInfo;
import com.bonus.material.purchase.service.IPurchaseMacodeInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 新购验收编号管理Service业务层处理
 * 
 * @author xsheng
 * @date 2024-10-16
 */
@Service
public class PurchaseMacodeInfoServiceImpl implements IPurchaseMacodeInfoService {

    @Autowired
    private PurchaseMacodeInfoMapper purchaseMacodeInfoMapper;

    @Resource
    private MachineMapper machineMapper;

    @Resource
    private PurchaseCheckDetailsMapper purchaseCheckDetailsMapper;

    /**
     * 查询新购验收编号管理
     * 
     * @param id 新购验收编号管理主键
     * @return 新购验收编号管理
     */
    @Override
    public PurchaseMacodeInfo selectPurchaseMacodeInfoById(Long id) {
        return purchaseMacodeInfoMapper.selectPurchaseMacodeInfoById(id);
    }

    /**
     * 查询新购验收编号管理列表
     * 
     * @param purchaseMacodeInfo 新购验收编号管理
     * @return 新购验收编号管理
     */
    @Override
    public List<PurchaseMacodeInfo> selectPurchaseMacodeInfoList(PurchaseMacodeInfo purchaseMacodeInfo) {
        List<PurchaseMacodeInfo> list = purchaseMacodeInfoMapper.selectPurchaseMacodeInfoList(purchaseMacodeInfo);
        return list;
    }

    /**
     * 新增新购验收编号管理
     * 
     * @param purchaseMacodeInfo 新购验收编号管理
     * @return 结果
     */
    @Override
    public int insertPurchaseMacodeInfo(PurchaseMacodeInfo purchaseMacodeInfo) {
        purchaseMacodeInfo.setCreateTime(DateUtils.getNowDate());
        try {
            return purchaseMacodeInfoMapper.insertPurchaseMacodeInfo(purchaseMacodeInfo);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 修改新购验收编号管理
     * 
     * @param purchaseMacodeInfo 新购验收编号管理
     * @return 结果
     */
    @Override
    public int updatePurchaseMacodeInfo(PurchaseMacodeInfo purchaseMacodeInfo) {
        purchaseMacodeInfo.setUpdateTime(DateUtils.getNowDate());
        try {
            return purchaseMacodeInfoMapper.updatePurchaseMacodeInfo(purchaseMacodeInfo);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 批量删除新购验收编号管理
     * 
     * @param ids 需要删除的新购验收编号管理主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePurchaseMacodeInfoByIds(List<Integer> ids) {
        for (Integer id : ids) {
            PurchaseMacodeInfo purchaseMacodeInfo = purchaseMacodeInfoMapper.selectPurchaseMacodeInfoById(Long.valueOf(id));
            List<PurchaseCheckDetails> purchaseCheckDetails = purchaseCheckDetailsMapper.selectPurchaseCheckDetailsList(new PurchaseCheckDetails()
                    .setTaskId(purchaseMacodeInfo.getTaskId()).setTypeId(purchaseMacodeInfo.getTypeId()));

            if (!purchaseCheckDetails.isEmpty()) {
                purchaseCheckDetailsMapper.updatePurchaseCheckDetails(new PurchaseCheckDetails()
                        .setId(purchaseCheckDetails.get(0).getId())
                        .setBindNum(purchaseCheckDetails.get(0).getBindNum().subtract(BigDecimal.ONE))
                );
            } else {
                throw new ServiceException("未找到对应验收详情数据，请检查数据是否正确");
            }

            int delSize = machineMapper.deleteMachineByMaCodeAndTypeId(purchaseMacodeInfo.getMaCode(), purchaseMacodeInfo.getTypeId());
            if (delSize != 1) {
                throw new ServiceException("机具物资表删除失败,物资清除:" + delSize + "条，数据已回滚");
            }
        }
        int delTotal = purchaseMacodeInfoMapper.updatePurchaseMacodeInfoByIds(ids);
        if (delTotal != ids.size()) {
            throw new ServiceException("删除失败,解绑:" + (ids.size() - delTotal) + "条,数据已回滚");
        }


        return delTotal;
    }

    /**
     * 删除新购验收编号管理信息
     * 
     * @param id 新购验收编号管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseMacodeInfoById(Long id) {
        return purchaseMacodeInfoMapper.deletePurchaseMacodeInfoById(id);
    }
}
