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.BuyerDeptFinancialDataMapper;
import com.ruoyi.buyerapi.domain.BuyerDeptFinancialData;
import com.ruoyi.buyerapi.service.IBuyerDeptFinancialDataService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 企业信息管理财务数据Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerDeptFinancialDataServiceImpl implements IBuyerDeptFinancialDataService {
    @Autowired
    private BuyerDeptFinancialDataMapper buyerDeptFinancialDataMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询企业信息管理财务数据
     *
     * @param id 企业信息管理财务数据主键
     * @return 企业信息管理财务数据
     */
    @Transactional
    @Override
    public BuyerDeptFinancialData selectBuyerDeptFinancialDataById(String id) {
        BuyerDeptFinancialData buyerDeptFinancialData = buyerDeptFinancialDataMapper.selectBuyerDeptFinancialDataById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerDeptFinancialData.setBuyerFileInfos(buyerFileInfos);
        return buyerDeptFinancialData;
    }

    /**
     * 查询企业信息管理财务数据列表
     *
     * @param buyerDeptFinancialData 企业信息管理财务数据
     * @return 企业信息管理财务数据
     */
    @Transactional
    @Override
    public List<BuyerDeptFinancialData> selectBuyerDeptFinancialDataList(BuyerDeptFinancialData buyerDeptFinancialData) {
        buyerDeptFinancialData.setDeptId(SecurityUtils.getCompanyId());
        List<BuyerDeptFinancialData> buyerDeptFinancialDatas = buyerDeptFinancialDataMapper.selectBuyerDeptFinancialDataList(buyerDeptFinancialData);
        // 查询关联文件
        for (BuyerDeptFinancialData deptFinancialData : buyerDeptFinancialDatas) {
            List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(deptFinancialData.getId());
            deptFinancialData.setBuyerFileInfos(buyerFileInfos);
        }
        return buyerDeptFinancialDatas;
    }

    /**
     * 新增企业信息管理财务数据
     *
     * @param buyerDeptFinancialData 企业信息管理财务数据
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuyerDeptFinancialData(BuyerDeptFinancialData buyerDeptFinancialData) {
        buyerDeptFinancialData.setCreateTime(DateUtils.getNowDate());
        buyerDeptFinancialData.setDeptId(SecurityUtils.getCompanyId());
        // 查询当前年度是否有数据
        BuyerDeptFinancialData buyerDeptFinancialDataEntity = new BuyerDeptFinancialData();
        buyerDeptFinancialDataEntity.setDeptId(buyerDeptFinancialData.getDeptId());
        buyerDeptFinancialDataEntity.setYear(buyerDeptFinancialData.getYear());
        List<BuyerDeptFinancialData> buyerDeptFinancialDatas = buyerDeptFinancialDataMapper.selectBuyerDeptFinancialDataList(buyerDeptFinancialDataEntity);
        // 当前年度有数据则删除原数据
        if (buyerDeptFinancialDatas.size() > 0) {
            for (BuyerDeptFinancialData deptFinancialData : buyerDeptFinancialDatas) {
                buyerDeptFinancialDataMapper.deleteBuyerDeptFinancialDataById(deptFinancialData.getId());
            }
        }
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDeptFinancialData.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(buyerDeptFinancialData.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        // 新增数据
        return buyerDeptFinancialDataMapper.insertBuyerDeptFinancialData(buyerDeptFinancialData);
    }

    /**
     * 修改企业信息管理财务数据
     *
     * @param buyerDeptFinancialData 企业信息管理财务数据
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBuyerDeptFinancialData(BuyerDeptFinancialData buyerDeptFinancialData) {
        buyerDeptFinancialData.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDeptFinancialData.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(buyerDeptFinancialData.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDeptFinancialData.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        } else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDeptFinancialData.getId());
        }
        return buyerDeptFinancialDataMapper.updateBuyerDeptFinancialData(buyerDeptFinancialData);
    }

    /**
     * 批量删除企业信息管理财务数据
     *
     * @param ids 需要删除的企业信息管理财务数据主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDeptFinancialDataByIds(String[] ids) {
        return buyerDeptFinancialDataMapper.deleteBuyerDeptFinancialDataByIds(ids);
    }

    /**
     * 删除企业信息管理财务数据信息
     *
     * @param id 企业信息管理财务数据主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDeptFinancialDataById(String id) {
        return buyerDeptFinancialDataMapper.deleteBuyerDeptFinancialDataById(id);
    }
}
