package com.mortals.xhx.module.staff.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.framework.util.SecurityUtil;
import com.mortals.xhx.base.system.role.model.RoleUserQuery;
import com.mortals.xhx.base.system.role.service.RoleUserService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.module.staff.dao.StaffDao;
import com.mortals.xhx.module.staff.dao.ibatis.StaffDaoImpl;
import com.mortals.xhx.module.staff.dao.ibatis.StaffLeaveDaoImpl;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.model.vo.StaffInfoVo;
import com.mortals.xhx.module.staff.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * StaffService
 * 员工基本信息 service实现
 *
 * @author zxfei
 * @date 2023-04-07
 */
@Service("staffService")
public class StaffServiceImpl extends AbstractCRUDCacheServiceImpl<StaffDao, StaffEntity, Long> implements StaffService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleUserService roleUserService;

    @Override
    protected String getExtKey(StaffEntity data) {
        //工号作为redis 扩展key
        return StrUtil.padPre(data.getWorkNum(), 8, "0");
        //  return "1000"+data.getWorkNum();
    }

    @Override
    protected void saveBefore(StaffEntity entity, Context context) throws AppException {
        int count = this.count(new StaffQuery().workNum(entity.getWorkNum()), context);
        if(count>0){
            throw new AppException("工号重复！");
        }
    }

    @Override
    public StaffInfoVo queryAll() {
        StaffInfoVo staffInfoVo = new StaffInfoVo();
        //在职
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setStatusList(Arrays.asList(StaffSatusEnum.正式.getValue(), StaffSatusEnum.试用.getValue()));
        List<StaffEntity> staffEntities = this.find(staffQuery);
        staffInfoVo.setInWorkStaff(staffEntities.size());
        //全职
        Map<Integer, Long> collect = staffEntities.stream().collect(Collectors.groupingBy(x -> x.getStaffType(), Collectors.counting()));
        staffInfoVo.setFullStaff(collect.getOrDefault(StaffTypeEnum.全职.getValue(), 0L).intValue());
        //实习
        staffInfoVo.setPricateStaff(collect.getOrDefault(StaffTypeEnum.实习.getValue(), 0L).intValue());
        //兼职
        staffInfoVo.setConcurrentlyStaff(collect.getOrDefault(StaffTypeEnum.兼职.getValue(), 0L).intValue());
        //试用
        staffInfoVo.setOnTrialStaff(this.find(new StaffQuery().status(StaffSatusEnum.试用.getValue())).size());
        //正式
        staffInfoVo.setFormalStaff(this.find(new StaffQuery().status(StaffSatusEnum.正式.getValue())).size());
        //待离职
        staffInfoVo.setResignationStaff(this.find(new StaffQuery().status(StaffSatusEnum.离职.getValue())).size());
        return staffInfoVo;
    }


    @Override
    public int queryAllPerson() {
        StaffQuery staffQuery = new StaffQuery();
        staffQuery.setStatusList(Arrays.asList(StaffSatusEnum.正式.getValue(), StaffSatusEnum.试用.getValue()));
        List<StaffEntity> staffEntities = this.find(staffQuery);
        return staffEntities.size();
    }


    @Override
    protected void saveAfter(StaffEntity entity, Context context) throws AppException {
        try {
            UserEntity userEntity = new UserEntity();

            if (ObjectUtils.isEmpty(entity.getLoginName())) {
                //姓名加工号作为登录名
                entity.setLoginName(entity.getName() + entity.getWorkNum());
            }
            if (ObjectUtils.isEmpty(entity.getLoginPwd())) {
                //设置初始密码
                entity.setLoginPwd("123456");
            }

            userEntity.setLoginName(StrUtil.cleanBlank(entity.getLoginName()));
            userEntity.setRealName(entity.getName());
            userEntity.setUserType(UserType.WORK_PERSON.getValue());
            userEntity.setSiteIds("1");
            userEntity.setAreaCodes("511500000000");
            userEntity.setMobile(entity.getPhoneNumber());
            userEntity.setStatus(UserStatus.NORMAL.getValue());
            userEntity.setCreateUserId(this.getContextUserId(context));
            userEntity.setCreateTime(new Date());
            userEntity.setCustomerId(entity.getId());

            userEntity.setLoginPwd(SecurityUtil.md5DoubleEncoding(entity.getLoginPwd()));
            int insert = userService.getUserDao().insert(userEntity);
            if (insert > 0) {
                RoleUserQuery roleUserQuery = new RoleUserQuery();
                roleUserQuery.setUserId(userEntity.getId());
                roleUserQuery.setRoleIdList(Arrays.asList(4L));
                roleUserService.assignRoleToUser(roleUserQuery);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        super.saveAfter(entity, context);
    }


    public static void main(String[] args) {


        System.out.println("1" + StrUtil.padPre("125", 7, "0"));

    }
}