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.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.BuyerDeptEmployeeMapper;
import com.ruoyi.buyerapi.domain.BuyerDeptEmployee;
import com.ruoyi.buyerapi.service.IBuyerDeptEmployeeService;

/**
 * 企业信息员工Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-22
 */
@Service
public class BuyerDeptEmployeeServiceImpl implements IBuyerDeptEmployeeService
{
    @Autowired
    private BuyerDeptEmployeeMapper buyerDeptEmployeeMapper;

    @Autowired
    private BuyerFileInfoMapper buyerFileInfoMapper;

    /**
     * 查询企业信息员工
     *
     * @param id 企业信息员工主键
     * @return 企业信息员工
     */
    @Override
    public BuyerDeptEmployee selectBuyerDeptEmployeeById(String id)
    {
        BuyerDeptEmployee buyerDeptEmployee = buyerDeptEmployeeMapper.selectBuyerDeptEmployeeById(id);
        // 查询关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerFileInfoMapper.selectBuyerFileInfoByLinkId(id);
        buyerDeptEmployee.setBuyerFileInfos(buyerFileInfos);
        return buyerDeptEmployee;
    }

    /**
     * 查询企业信息员工列表
     *
     * @param buyerDeptEmployee 企业信息员工
     * @return 企业信息员工
     */
    @Override
    public List<BuyerDeptEmployee> selectBuyerDeptEmployeeList(BuyerDeptEmployee buyerDeptEmployee)
    {
        buyerDeptEmployee.setDeptId(SecurityUtils.getCompanyId());
        return buyerDeptEmployeeMapper.selectBuyerDeptEmployeeList(buyerDeptEmployee);
    }

    /**
     * 查询项目员工列表
     *
     * @param buyerDeptEmployee 企业信息员工
     * @return 企业信息员工
     */
    @Override
    public List<BuyerDeptEmployee> selectBuyerDeptEmployeeLists(BuyerDeptEmployee buyerDeptEmployee)
    {
        buyerDeptEmployee.setDeptId(SecurityUtils.getCompanyId());
        return buyerDeptEmployeeMapper.selectBuyerDeptEmployeeLists(buyerDeptEmployee);
    }

    /**
     * 新增企业信息员工
     *
     * @param buyerDeptEmployee 企业信息员工
     * @return 结果
     */
    @Override
    public int insertBuyerDeptEmployee(BuyerDeptEmployee buyerDeptEmployee)
    {
        buyerDeptEmployee.setCreateTime(DateUtils.getNowDate());
        buyerDeptEmployee.setDeptId(SecurityUtils.getCompanyId());
        // 新增关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDeptEmployee.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(buyerDeptEmployee.getId());
            }
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }
        return buyerDeptEmployeeMapper.insertBuyerDeptEmployee(buyerDeptEmployee);
    }

    /**
     * 修改企业信息员工
     *
     * @param buyerDeptEmployee 企业信息员工
     * @return 结果
     */
    @Override
    public int updateBuyerDeptEmployee(BuyerDeptEmployee buyerDeptEmployee)
    {
        buyerDeptEmployee.setUpdateTime(DateUtils.getNowDate());
        // 修改关联文件
        List<BuyerFileInfo> buyerFileInfos = buyerDeptEmployee.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(buyerDeptEmployee.getId());
            }
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDeptEmployee.getId());
            buyerFileInfoMapper.batchInsertBuyerFileInfo(buyerFileInfos);
        }else if (buyerFileInfos != null && buyerFileInfos.size() == 0) {
            buyerFileInfoMapper.deleteBuyerFileInfoByLinkId(buyerDeptEmployee.getId());
        }
        return buyerDeptEmployeeMapper.updateBuyerDeptEmployee(buyerDeptEmployee);
    }

    /**
     * 批量删除企业信息员工
     *
     * @param ids 需要删除的企业信息员工主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDeptEmployeeByIds(String[] ids)
    {
        return buyerDeptEmployeeMapper.deleteBuyerDeptEmployeeByIds(ids);
    }

    /**
     * 删除企业信息员工信息
     *
     * @param id 企业信息员工主键
     * @return 结果
     */
    @Override
    public int deleteBuyerDeptEmployeeById(String id)
    {
        return buyerDeptEmployeeMapper.deleteBuyerDeptEmployeeById(id);
    }
}
