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


import com.dcsoft.common.core.text.Convert;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.consume.domain.ConsumeGroup;
import com.dcsoft.system.consume.domain.ConsumeGroupUser;
import com.dcsoft.system.consume.mapper.ConsumeGroupMapper;
import com.dcsoft.system.consume.mapper.ConsumeGroupUserMapper;
import com.dcsoft.system.consume.service.IConsumeGroupService;
import com.dcsoft.system.domain.SysPeople;

import com.dcsoft.system.service.ISysPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 考勤组Service业务层处理
 *
 * @author dcsoft
 * @date 2023-01-05
 */
@Service
public class ConsumeGroupServiceImpl implements IConsumeGroupService
{
    @Autowired
    private ConsumeGroupMapper consumeGroupMapper;

    @Autowired
    private ConsumeGroupUserMapper consumeGroupUserMapper;


    @Autowired
    private ISysPeopleService sysPeopleService;

    /**
     * 查询考勤组
     *
     * @param groupId 考勤组主键
     * @return 考勤组
     */
    @Override
    public ConsumeGroup selectConsumeGroupByGroupId(Long groupId)
    {
        ConsumeGroup group=consumeGroupMapper.selectConsumeGroupByGroupId(groupId);
        ConsumeGroupUser consumeGroupUser=new ConsumeGroupUser();
        consumeGroupUser.setGroupId(groupId);
        List<ConsumeGroupUser> userList=consumeGroupUserMapper.selectConsumeGroupUserList(consumeGroupUser);
        String userids="";
        for(ConsumeGroupUser u:userList){
            userids+=u.getUserId()+",";
        }
        if(StringUtils.isNotEmpty(userids)){
            userids=userids.substring(0,userids.length()-1);
            Long[] userIds= Convert.toLongArray(userids);
            group.setUserIds(userIds);
        }
        return group;
    }

    /**
     * 查询考勤组列表
     *
     * @param consumeGroup 考勤组
     * @return 考勤组
     */
    @Override
    public List<ConsumeGroup> selectConsumeGroupList(ConsumeGroup consumeGroup)
    {
        return consumeGroupMapper.selectConsumeGroupList(consumeGroup);
    }

    /**
     * 新增考勤组
     *
     * @param consumeGroup 考勤组
     * @return 结果
     */
    @Override
    public int insertConsumeGroup(ConsumeGroup consumeGroup)
    {
        consumeGroup.setCreateTime(DateUtils.getNowDate());
        int i=consumeGroupMapper.insertConsumeGroup(consumeGroup);
        // 新增用户岗位关联
        insertconsumeGroupUser(consumeGroup);
        return i;
    }

    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertconsumeGroupUser(ConsumeGroup consumeGroup)
    {
        Long[] userIds = consumeGroup.getUserIds();
        if (StringUtils.isNotEmpty(userIds))
        {
            // 新增用户与岗位管理
            List<ConsumeGroupUser> list = new ArrayList<ConsumeGroupUser>(userIds.length);
            for (Long postId : userIds)
            {
                consumeGroupUserMapper.deleteConsumeGroupUserByUserId(postId);
                ConsumeGroupUser up = new ConsumeGroupUser();
                up.setUserId(postId);
                up.setGroupId(consumeGroup.getGroupId());
                list.add(up);
            }
            consumeGroupUserMapper.batchGroupUser(list);
        }else{//没有考勤人员时，为全部门人员考核
            List<ConsumeGroupUser> list =new ArrayList<>();
            SysPeople sysPeople=new SysPeople();
            sysPeople.setBranchId(consumeGroup.getDeptId());
            sysPeople.setDelFlag(0L);
            List<SysPeople>  peopleList=sysPeopleService.selectSysPeopleList(sysPeople);
            for (SysPeople people : peopleList)
            {
                consumeGroupUserMapper.deleteConsumeGroupUserByUserId(people.getId());
                ConsumeGroupUser up = new ConsumeGroupUser();
                up.setUserId(people.getId());
                up.setGroupId(consumeGroup.getGroupId());
                list.add(up);
            }
            consumeGroupUserMapper.batchGroupUser(list);
        }
    }

    /**
     * 修改考勤组
     *
     * @param consumeGroup 考勤组
     * @return 结果
     */
    @Override
    public int updateConsumeGroup(ConsumeGroup consumeGroup)
    {
        consumeGroup.setUpdateTime(DateUtils.getNowDate());
        consumeGroupUserMapper.deleteConsumeGroupUserByGroupId(consumeGroup.getGroupId());
        // 新增用户岗位关联
        insertconsumeGroupUser(consumeGroup);
        return consumeGroupMapper.updateConsumeGroup(consumeGroup);
    }

    /**
     * 批量删除考勤组
     *
     * @param groupIds 需要删除的考勤组主键
     * @return 结果
     */
    @Override
    public int deleteConsumeGroupByGroupIds(Long[] groupIds)
    {
        for (Long groupId : groupIds)
        {
            consumeGroupUserMapper.deleteConsumeGroupUserByGroupId(groupId);
        }
        return consumeGroupMapper.deleteConsumeGroupByGroupIds(groupIds);
    }

    /**
     * 删除考勤组信息
     *
     * @param groupId 考勤组主键
     * @return 结果
     */
    @Override
    public int deleteConsumeGroupByGroupId(Long groupId)
    {
        return consumeGroupMapper.deleteConsumeGroupByGroupId(groupId);
    }
}
