package com.dcsoft.system.consume.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dcsoft.common.core.text.Convert;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.common.datascope.annotation.DataScope;
import com.dcsoft.system.consume.domain.*;
import com.dcsoft.system.consume.mapper.ConsumePeopleDayMapper;
import com.dcsoft.system.consume.mapper.ConsumeReportMapper;
import com.dcsoft.system.consume.mapper.ConsumeReportUserMapper;
import com.dcsoft.system.consume.service.IConsumeReportService;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.service.ISysPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 三方上报餐补信息Service业务层处理
 *
 * @author dcsoft
 * @date 2024-10-24
 */
@Service
public class ConsumeReportServiceImpl implements IConsumeReportService
{
    @Autowired
    private ConsumeReportMapper consumeReportMapper;

    @Autowired
    private ConsumeReportUserMapper consumeReportUserMapper;

    @Autowired
    private ISysPeopleService sysPeopleService;

    @Autowired
    private ConsumePeopleDayMapper consumePeopleDayMapper;

    /**
     * 查询三方上报餐补信息
     *
     * @param id 三方上报餐补信息主键
     * @return 三方上报餐补信息
     */
    @Override
    public ConsumeReport selectConsumeReportById(Long id)
    {
        ConsumeReport report=consumeReportMapper.selectConsumeReportById(id);
        ConsumeReportUser consumeReportUser=new ConsumeReportUser();
        consumeReportUser.setReportId(id);
        List<ConsumeReportUser> userList=consumeReportUserMapper.selectConsumeReportUserList(consumeReportUser);
        String userids="";
        for(ConsumeReportUser u:userList){
            userids+=u.getUserId()+",";
        }
        if(StringUtils.isNotEmpty(userids)){
            userids=userids.substring(0,userids.length()-1);
            Long[] userIds= Convert.toLongArray(userids);
            report.setUserIds(userIds);
        }
        return report;
    }

    /**
     * 查询三方上报餐补信息列表
     *
     * @param consumeReport 三方上报餐补信息
     * @return 三方上报餐补信息
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ConsumeReport> selectConsumeReportList(ConsumeReport consumeReport)
    {
        return consumeReportMapper.selectConsumeReportList(consumeReport);
    }

    /**
     * 新增三方上报餐补信息
     *
     * @param consumeReport 三方上报餐补信息
     * @return 结果
     */
    @Override
    public int insertConsumeReport(ConsumeReport consumeReport)
    {
        consumeReport.setCreateTime(DateUtils.getNowDate());
        int i=consumeReportMapper.insertConsumeReport(consumeReport);
        // 新增用户岗位关联
        insertConsumeReportUser(consumeReport);
        return i;
    }

    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertConsumeReportUser(ConsumeReport consumeReport)
    {
        Long[] userIds = consumeReport.getUserIds();

        if (StringUtils.isNotEmpty(userIds))
        {
            // 新增用户与岗位管理
            List<ConsumeReportUser> list = new ArrayList<ConsumeReportUser>(userIds.length);
            for (Long postId : userIds)
            {
                ConsumeReportUser up = new ConsumeReportUser();
                up.setUserId(postId);
                up.setReportId(consumeReport.getId());
                list.add(up);
                SysPeople people=sysPeopleService.selectSysPeopleById(postId);
                ConsumePeopleDay consumePeopleDay=new ConsumePeopleDay();
                consumePeopleDay.setConsumeTime(consumeReport.getConsumeTime());
                consumePeopleDay.setConsumeInterval(consumeReport.getConsumeInterval());
                consumePeopleDay.setType(consumeReport.getType());
                consumePeopleDay.setCanteen(consumeReport.getCanteen());
                consumePeopleDay.setAccountId(postId+"");
                consumePeopleDay.setReportId(consumeReport.getId());
                consumePeopleDay.setEmpId(people.getGh());
                consumePeopleDayMapper.insertConsumePeopleDay(consumePeopleDay);
            }
            consumeReportUserMapper.batchReportUser(list);
        }else{//consumeReportUserMapper，为全部门人员考核
            List<ConsumeReportUser> list =new ArrayList<>();
            SysPeople sysPeople=new SysPeople();
            sysPeople.setBranchId(consumeReport.getBranchId());
            sysPeople.setDelFlag(0L);
            List<SysPeople>  peopleList=sysPeopleService.selectSysPeopleList(sysPeople);
            for (SysPeople people : peopleList)
            {
                ConsumeReportUser up = new ConsumeReportUser();
                up.setUserId(people.getId());
                up.setReportId(consumeReport.getId());
                list.add(up);
                ConsumePeopleDay consumePeopleDay=new ConsumePeopleDay();
                consumePeopleDay.setConsumeTime(consumeReport.getConsumeTime());
                consumePeopleDay.setConsumeInterval(consumeReport.getConsumeInterval());
                consumePeopleDay.setType(consumeReport.getType());
                consumePeopleDay.setCanteen(consumeReport.getCanteen());
                consumePeopleDay.setAccountId(people.getId()+"");
                consumePeopleDay.setEmpId(people.getGh());
                consumePeopleDay.setReportId(consumeReport.getId());
                consumePeopleDayMapper.insertConsumePeopleDay(consumePeopleDay);
            }
            consumeReportUserMapper.batchReportUser(list);
        }
    }


    /**
     * 修改三方上报餐补信息
     *
     * @param consumeReport 三方上报餐补信息
     * @return 结果
     */
    @Override
    public int updateConsumeReport(ConsumeReport consumeReport)
    {
        consumeReport.setUpdateTime(DateUtils.getNowDate());
        consumePeopleDayMapper.deleteConsumePeopleDayByReportId(consumeReport.getId());
        consumeReportUserMapper.deleteConsumeReportUserByReportId(consumeReport.getId());
        insertConsumeReportUser(consumeReport);
        return consumeReportMapper.updateConsumeReport(consumeReport);
    }

    /**
     * 批量删除三方上报餐补信息
     *
     * @param ids 需要删除的三方上报餐补信息主键
     * @return 结果
     */
    @Override
    public int deleteConsumeReportByIds(Long[] ids)
    {
        for (Long groupId : ids)
        {
            consumePeopleDayMapper.deleteConsumePeopleDayByReportId(groupId);
            consumeReportUserMapper.deleteConsumeReportUserByReportId(groupId);
        }
        return consumeReportMapper.deleteConsumeReportByIds(ids);
    }

    /**
     * 删除三方上报餐补信息信息
     *
     * @param id 三方上报餐补信息主键
     * @return 结果
     */
    @Override
    public int deleteConsumeReportById(Long id)
    {
        consumePeopleDayMapper.deleteConsumePeopleDayByReportId(id);
        consumeReportUserMapper.deleteConsumeReportUserByReportId(id);
        return consumeReportMapper.deleteConsumeReportById(id);
    }
}
