package com.staffing.custom.service.impl;

import java.util.*;

import com.staffing.common.core.domain.entity.SysUser;
import com.staffing.common.exception.ServiceException;
import com.staffing.common.utils.DateUtils;
import com.staffing.common.utils.SecurityUtils;
import com.staffing.common.utils.StringUtils;
import com.staffing.custom.domain.StEmployeeFundRecord;
import com.staffing.custom.domain.StTaskSign;
import com.staffing.custom.mapper.StEmployeeFundRecordMapper;
import com.staffing.custom.mapper.StTaskSignMapper;
import com.staffing.custom.service.IStTaskService;
import com.staffing.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import com.staffing.custom.mapper.StEmployeeMapper;
import com.staffing.custom.domain.StEmployee;
import com.staffing.custom.service.IStEmployeeService;

import static com.staffing.common.utils.PageUtils.startPage;

/**
 * 员工Service业务层处理
 * 
 * @author staffing
 * @date 2024-08-27
 */
@Service
public class StEmployeeServiceImpl implements IStEmployeeService 
{
    @Autowired
    private StEmployeeMapper stEmployeeMapper;

    @Autowired
    private StTaskSignMapper stTaskSignMapper;

    @Autowired
    private StEmployeeFundRecordMapper stEmployeeFundRecordMapper;

    @Autowired
    private IStTaskService stTaskService;

    private static final Logger log = LoggerFactory.getLogger(StEmployeeServiceImpl.class);

    /**
     * 查询员工
     * 
     * @param id 员工主键
     * @return 员工
     */
    @Override
    public StEmployee selectStEmployeeById(Long id)
    {
        return stEmployeeMapper.selectStEmployeeById(id);
    }

    public StEmployee selectStEmployeeByPhone(String phone){
        return stEmployeeMapper.selectStEmployeeByPhone(phone);
    }

    /**
     * 查询员工列表
     * 
     * @param stEmployee 员工
     * @return 员工
     */
    @Override
    public List<StEmployee> selectStEmployeeList(StEmployee stEmployee)
    {
        return stEmployeeMapper.selectStEmployeeList(stEmployee);
    }
    @Override
    public List<StTaskSign> selectStEmployeeListAndSign(StEmployee stEmployee)
    {
        Map<Long, StTaskSign> result = new LinkedHashMap();
        StTaskSign param = new StTaskSign();
        param.setTaskId(stEmployee.getTaskId());
        param.setOrderType(1);
        List<StTaskSign> stTaskSignsTemp = stTaskSignMapper.selectStTaskSignList(param);
        for (StTaskSign stTaskSign : stTaskSignsTemp) {
            stTaskSign.setStatus(0);
            result.put(stTaskSign.getEmployeeId(), stTaskSign);
        }
        startPage();

        List<StEmployee> stEmployees = stEmployeeMapper.selectStEmployeeList(stEmployee);
        for (StEmployee stEmployee1 : stEmployees) {
            if (result.containsKey(stEmployee1.getId())){
                continue;
            }
            StTaskSign stTaskSign = new StTaskSign();
            stTaskSign.setTaskId(stEmployee1.getTaskId());
            stTaskSign.setEmployeeName(stEmployee1.getRealName());
            stTaskSign.setPhone(stEmployee1.getPhone());
            stTaskSign.setIdCard(stEmployee1.getIdCard());
            stTaskSign.setEmployeeId(stEmployee1.getId());
            stTaskSign.setStatus(2);
            result.put(stEmployee1.getId(), stTaskSign);
        }

        return new ArrayList<>(result.values());
    }

    /**
     * 新增员工
     * 
     * @param stEmployee 员工
     * @return 结果
     */
    @Override
    public int insertStEmployee(StEmployee stEmployee)
    {
        return stEmployeeMapper.insertStEmployee(stEmployee);
    }

    /**
     * 修改员工
     * 
     * @param stEmployee 员工
     * @return 结果
     */
    @Override
    public int updateStEmployee(StEmployee stEmployee)
    {
        stTaskService.updateTaskUserInfo(stEmployee);

        stEmployee.setUpdateTime(DateUtils.getNowDate());
        return stEmployeeMapper.updateStEmployee(stEmployee);
    }

    @Override
    public int updateStEmployeeOrNull(StEmployee stEmployee)
    {
        stTaskService.updateTaskUserInfo(stEmployee);

        stEmployee.setUpdateTime(DateUtils.getNowDate());
        stEmployeeMapper.updateStEmployeeOrNull(stEmployee);
        return 1;
    }


    /**
     * 批量删除员工
     * 
     * @param ids 需要删除的员工主键
     * @return 结果
     */
    @Override
    public int deleteStEmployeeByIds(Long[] ids)
    {
        return stEmployeeMapper.deleteStEmployeeByIds(ids);
    }

    /**
     * 删除员工信息
     * 
     * @param id 员工主键
     * @return 结果
     */
    @Override
    public int deleteStEmployeeById(Long id)
    {
        return stEmployeeMapper.deleteStEmployeeById(id);
    }

    @Override
    public String importStEmployee(List<StEmployee> stEmployees)
    {
        if (StringUtils.isNull(stEmployees) || stEmployees.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (StEmployee record : stEmployees)
        {
            try
            {
                record.setCreateTime(new Date());
                record.setEmployeeType("灵活用工".equals(record.getEmployeeTypeStr()) ? 0 : 1);

                if ("男".equals(record.getSexStr())){
                    record.setSex(0);
                }else if ("女".equals(record.getSexStr())) {
                    record.setSex(1);
                }else{
                    record.setSex(2);
                }
                int i = stEmployeeMapper.save(record);
                if (i > 0){
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据导入修改成功");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
