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

import java.util.Date;
import java.util.List;

import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.face.domain.FaceStudent;
import com.dcsoft.system.kq.domain.KqAdjust;
import com.dcsoft.system.kq.mapper.KqAdjustMapper;
import com.dcsoft.system.kq.service.IKqAdjustService;
import com.dcsoft.system.kq.service.IKqGatherService;
import com.dcsoft.system.mapper.SysBranchMapper;
import com.dcsoft.system.mapper.SysPeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 出勤调整Service业务层处理
 *
 * @author nichun
 * @date 2023-06-09
 */
@Service
public class KqAdjustServiceImpl implements IKqAdjustService
{
    @Autowired
    private KqAdjustMapper kqAdjustMapper;

    @Autowired
    private SysPeopleMapper peopleMapper;

    @Autowired
    private SysBranchMapper branchMapper;

    @Resource
    private IKqGatherService kqGatherService;


    /**
     * 查询出勤调整
     *
     * @param adjustId 出勤调整主键
     * @return 出勤调整
     */
    @Override
    public KqAdjust selectKqAdjustByAdjustId(Long adjustId)
    {
        return kqAdjustMapper.selectKqAdjustByAdjustId(adjustId);
    }

    /**
     * 查询出勤调整列表
     *
     * @param kqAdjust 出勤调整
     * @return 出勤调整
     */
    @Override
    public List<KqAdjust> selectKqAdjustList(KqAdjust kqAdjust)
    {
        return kqAdjustMapper.selectKqAdjustList(kqAdjust);
    }

    /**
     * 新增出勤调整
     *
     * @param kqAdjust 出勤调整
     * @return 结果
     */
    @Override
    public int insertKqAdjust(KqAdjust kqAdjust)
    {
        if("5".equals(kqAdjust.getType())) {
            kqGatherService.saveCardReplacement(kqAdjust);
        }

        kqAdjust.setCreateTime(DateUtils.getNowDate());
        return kqAdjustMapper.insertKqAdjust(kqAdjust);
    }

    /**
     * 修改出勤调整
     *
     * @param kqAdjust 出勤调整
     * @return 结果
     */
    @Override
    public int updateKqAdjust(KqAdjust kqAdjust)
    {
        kqAdjust.setUpdateTime(DateUtils.getNowDate());
        return kqAdjustMapper.updateKqAdjust(kqAdjust);
    }

    /**
     * 批量删除出勤调整
     *
     * @param adjustIds 需要删除的出勤调整主键
     * @return 结果
     */
    @Override
    public int deleteKqAdjustByAdjustIds(Long[] adjustIds)
    {
        return kqAdjustMapper.deleteKqAdjustByAdjustIds(adjustIds);
    }

    /**
     * 删除出勤调整信息
     *
     * @param adjustId 出勤调整主键
     * @return 结果
     */
    @Override
    public int deleteKqAdjustByAdjustId(Long adjustId)
    {
        return kqAdjustMapper.deleteKqAdjustByAdjustId(adjustId);
    }



    /**
     * 导入用户数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importkqAdjust(List<KqAdjust> userList, boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i=0;
        for (KqAdjust user : userList)
        {
            i++;
            try
            {
                // 验证是否存在这个用户
                Long code=new Date().getTime()+i;
                user.setCode(code+"");
                KqAdjust u = kqAdjustMapper.selectKqAdjustByAdjustCode(user.getCode());
                SysPeople people=peopleMapper.selectSysPeopleByBranchUser(user.getDeptName(),user.getUserName());
                if(people!=null){
                    user.setUserId(people.getId());
                    user.setDeptId(people.getBranchId());
                }
                if (StringUtils.isNull(u))
                {
                    user.setCreateBy(operName);
                    this.insertKqAdjust(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、申请单号 " + user.getCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    user.setAdjustId(u.getAdjustId());
                    user.setUpdateBy(operName);
                    this.updateKqAdjust(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、申请单号 " + user.getCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、申请单号 " + user.getCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、申请单号 " + user.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
