package com.dcsoft.system.kq.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.domain.SysPeople;
import com.dcsoft.system.kq.domain.KqGroup;
import com.dcsoft.system.kq.domain.KqGroupUser;
import com.dcsoft.system.kq.mapper.KqGroupMapper;
import com.dcsoft.system.kq.mapper.KqGroupUserMapper;
import com.dcsoft.system.kq.service.IKqGroupService;
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 KqGroupServiceImpl implements IKqGroupService
{
    @Autowired
    private KqGroupMapper kqGroupMapper;

    @Autowired
    private KqGroupUserMapper kqGroupUserMapper;


    @Autowired
    private ISysPeopleService sysPeopleService;

    /**
     * 查询考勤组
     *
     * @param groupId 考勤组主键
     * @return 考勤组
     */
    @Override
    public KqGroup selectKqGroupByGroupId(Long groupId)
    {
        KqGroup group=kqGroupMapper.selectKqGroupByGroupId(groupId);
        KqGroupUser kqGroupUser=new KqGroupUser();
        kqGroupUser.setGroupId(groupId);
        List<KqGroupUser> userList=kqGroupUserMapper.selectKqGroupUserList(kqGroupUser);
        String userids="";
        for(KqGroupUser 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 kqGroup 考勤组
     * @return 考勤组
     */
    @Override
    public List<KqGroup> selectKqGroupList(KqGroup kqGroup)
    {
        return kqGroupMapper.selectKqGroupList(kqGroup);
    }

    /**
     * 新增考勤组
     *
     * @param kqGroup 考勤组
     * @return 结果
     */
    @Override
    public int insertKqGroup(KqGroup kqGroup)
    {
        kqGroup.setCreateTime(DateUtils.getNowDate());
        // 新增用户岗位关联
        insertkqGroupUser(kqGroup);
        return kqGroupMapper.insertKqGroup(kqGroup);
    }

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

    /**
     * 修改考勤组
     *
     * @param kqGroup 考勤组
     * @return 结果
     */
    @Override
    public int updateKqGroup(KqGroup kqGroup)
    {
        kqGroup.setUpdateTime(DateUtils.getNowDate());
        kqGroupUserMapper.deleteKqGroupUserByGroupId(kqGroup.getGroupId());
        // 新增用户岗位关联
        insertkqGroupUser(kqGroup);
        return kqGroupMapper.updateKqGroup(kqGroup);
    }

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

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