package com.ruoyi.buyerapi.service.impl;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.buyerapi.domain.BuyerFileInfo;
import com.ruoyi.buyerapi.domain.BuyerGoods;
import com.ruoyi.buyerapi.mapper.BuyerFileInfoMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buyerapi.mapper.BuyerDesignRecordMapper;
import com.ruoyi.buyerapi.domain.BuyerDesignRecord;
import com.ruoyi.buyerapi.service.IBuyerDesignRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 装备调试设计记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerDesignRecordServiceImpl implements IBuyerDesignRecordService {
    @Autowired
    private BuyerDesignRecordMapper buyerDesignRecordMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询装备调试设计记录
     *
     * @param id 装备调试设计记录主键
     * @return 装备调试设计记录
     */
    @Transactional
    @Override
    public BuyerDesignRecord selectBuyerDesignRecordById(String id) {
        BuyerDesignRecord buyerDesignRecord = buyerDesignRecordMapper.selectBuyerDesignRecordById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerDesignRecord.setBuyerFileInfos(buyerFileInfos);
        return buyerDesignRecord;
    }

    /**
     * 查询装备调试设计记录列表
     *
     * @param buyerDesignRecord 装备调试设计记录
     * @return 装备调试设计记录
     */
    @Transactional
    @Override
    public List<BuyerDesignRecord> selectBuyerDesignRecordList(BuyerDesignRecord buyerDesignRecord) {
        buyerDesignRecord.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerDesignRecord> buyerDesignRecords = buyerDesignRecordMapper.selectBuyerDesignRecordList(buyerDesignRecord);
        // 查询关联文件
        for (BuyerDesignRecord designRecord : buyerDesignRecords) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(designRecord.getId());
            designRecord.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerDesignRecords;
    }

    /**
     * 统计装备调试设计记录列表
     *
     * @param buyerDesignRecord 装备调试设计记录
     * @return 装备调试设计记录
     */
    @Override
    public List<BuyerDesignRecord> selectStatistBuyerDesignRecordList(BuyerDesignRecord buyerDesignRecord) {
        buyerDesignRecord.setDeptId(SecurityUtils.getCompanyId());
        return buyerDesignRecordMapper.selectStatistBuyerDesignRecordList(buyerDesignRecord);
    }

    /**
     * 新增装备调试设计记录
     *
     * @param buyerDesignRecord 装备调试设计记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerDesignRecord(BuyerDesignRecord buyerDesignRecord) {
        buyerDesignRecord.setDeptId(SecurityUtils.getCompanyId());
        buyerDesignRecord.setCreateTime(DateUtils.getNowDate());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDesignRecord.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size()>0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setCreateTime(DateUtils.getNowDate());
                buyerFileInfo.setCreateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerDesignRecord.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerDesignRecordMapper.insertBuyerDesignRecord(buyerDesignRecord);
    }

    /**
     * 修改装备调试设计记录
     *
     * @param buyerDesignRecord 装备调试设计记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerDesignRecord(BuyerDesignRecord buyerDesignRecord) {
        buyerDesignRecord.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDesignRecord.getBuyerFileInfos();
        if (buyerFileInfos != null && buyerFileInfos.size()>0) {
            for (BuyerFileInfo buyerFileInfo : buyerFileInfos) {
                buyerFileInfo.setId(IdUtil.simpleUUID());
                buyerFileInfo.setUpdateTime(DateUtils.getNowDate());
                buyerFileInfo.setUpdateBy(SecurityUtils.getUsername());
                buyerFileInfo.setLinkId(buyerDesignRecord.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDesignRecord.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDesignRecord.getId());
        }
        return buyerDesignRecordMapper.updateBuyerDesignRecord(buyerDesignRecord);
    }

    /**
     * 批量删除装备调试设计记录
     *
     * @param ids 需要删除的装备调试设计记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDesignRecordByIds(String[] ids) {
        return buyerDesignRecordMapper.deleteBuyerDesignRecordByIds(ids);
    }

    /**
     * 删除装备调试设计记录信息
     *
     * @param id 装备调试设计记录主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDesignRecordById(String id) {
        return buyerDesignRecordMapper.deleteBuyerDesignRecordById(id);
    }
}
