package com.cool.store.service.impl;

import com.cool.store.dao.*;
import com.cool.store.dto.ehr.StaffBaseInfoDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.MessageEnum;
import com.cool.store.enums.UserRoleEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.TempUserDetailRequest;
import com.cool.store.service.PreparationService;
import com.cool.store.service.TempUserDetailService;
import com.cool.store.service.XfsgEhrService;
import com.cool.store.utils.StringUtil;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.vo.TempUserDetailInfoVO;
import com.cool.store.vo.TempUserDetailListVO;
import com.cool.store.vo.TempUserDetailVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author byd
 * @date 2024-04-29 16:14
 */
@Service
public class TempUserDetailServiceImpl implements TempUserDetailService {

    @Resource
    private TempUserDetailDAO tempUserDetailDAO;

    @Resource
    private ShopInfoDAO shopInfoDAO;

    @Resource
    private EnterpriseUserDAO enterpriseUserDAO;

    @Autowired
    private XfsgEhrService xfsgEhrService;

    @Autowired
    private ShopStageInfoDAO shopStageInfoDAO;

    @Autowired
    private CommonService commonService;

    @Autowired
    private EnterpriseUserRoleDao enterpriseUserRoleDao;

    @Autowired
    private PreparationService preparationService;

    @Autowired
    private RegionDao regionDao;

    @Override
    public TempUserDetailListVO userList(Long shopId) {
        List<TempUserDetailDO> tempUserDetailDOList = tempUserDetailDAO.userList(shopId);
        List<TempUserDetailVO> resultList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(tempUserDetailDOList)) {
            tempUserDetailDOList.forEach(tempUserDetailDO -> {
                TempUserDetailVO tempUserDetailVO = new TempUserDetailVO();
                tempUserDetailVO.setId(tempUserDetailDO.getId());
                tempUserDetailVO.setMobile(tempUserDetailDO.getMobile());
                tempUserDetailVO.setIdCard(tempUserDetailDO.getIdCard());
                tempUserDetailVO.setSource(tempUserDetailDO.getSource());
                tempUserDetailVO.setRegisterTime(tempUserDetailDO.getRegisterTime());
                tempUserDetailVO.setShopId(tempUserDetailDO.getShopId());
                tempUserDetailVO.setTrainTime(tempUserDetailDO.getTrainTime());
                tempUserDetailVO.setLineId(tempUserDetailDO.getLineId());
                tempUserDetailVO.setUsername(tempUserDetailDO.getUsername());
                tempUserDetailVO.setRegionId(tempUserDetailDO.getRegionId());
                tempUserDetailVO.setRoleId(tempUserDetailDO.getRoleId());
                resultList.add(tempUserDetailVO);
            });
        }
        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(shopId);
        if(shopInfoDO == null){
            throw new ServiceException("该店铺不存在");
        }
        TempUserDetailListVO tempUserDetailListVO = new TempUserDetailListVO();
        tempUserDetailListVO.setUserList(resultList);
        tempUserDetailListVO.setShopId(shopId);
        tempUserDetailListVO.setStoreNum(shopInfoDO.getStoreNum());
        ShopStageInfoDO shopStageInfoDO = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_5);
        if(shopStageInfoDO != null){
            tempUserDetailListVO.setShopSubStageStatus(shopStageInfoDO.getShopSubStageStatus());
        }
        return tempUserDetailListVO;
    }

    @Override
    public void addTempUserDetail(TempUserDetailRequest tempUserDetailRequest) {
        TempUserDetailDO tempUserDetailDO = tempUserDetailDAO.selectByIdCard(tempUserDetailRequest.getIdCard());
        if(tempUserDetailDO != null){
            throw new ServiceException("该员工已登记");
        }
        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(tempUserDetailRequest.getShopId());
        if(shopInfoDO == null){
            throw new ServiceException("该店铺不存在");
        }
        tempUserDetailDO = new TempUserDetailDO();
        tempUserDetailDO.setLineId(shopInfoDO.getLineId());
        tempUserDetailDO.setShopId(tempUserDetailRequest.getShopId());
        tempUserDetailDO.setRegionId(shopInfoDO.getRegionId());
        tempUserDetailDO.setMobile(tempUserDetailRequest.getPhone());
        tempUserDetailDO.setUsername(tempUserDetailRequest.getName());
        tempUserDetailDO.setSex(tempUserDetailRequest.getSex());
        tempUserDetailDO.setAge(tempUserDetailRequest.getAge());
        tempUserDetailDO.setIdCard(tempUserDetailRequest.getIdCard());
        tempUserDetailDO.setStatus(1);
        tempUserDetailDO.setEducational(tempUserDetailRequest.getHighestDegree());
        tempUserDetailDO.setRoleId(tempUserDetailRequest.getJobId());
        tempUserDetailDO.setIdCardNegativeUrl(tempUserDetailRequest.getEmblemPhoto());
        tempUserDetailDO.setIdCardPositiveUrl(tempUserDetailRequest.getIdNumPhoto());
        tempUserDetailDO.setHealthCertificateUrl(tempUserDetailRequest.getHealthCertificate());
        tempUserDetailDO.setRegisterTime(tempUserDetailRequest.getRegisterTime());
        tempUserDetailDO.setTrainTime(tempUserDetailRequest.getTrainTime());
        tempUserDetailDO.setCompletionCertificate(tempUserDetailRequest.getCompletionCertificate());
        tempUserDetailDO.setSubmitTime(new Date());
        tempUserDetailDO.setSource("create");
        tempUserDetailDAO.insertSelective(tempUserDetailDO);

        //登记人员，培训状态由未开始到培训中
//        ShopStageInfoDO shopStageInfoDO = shopStageInfoDAO.getShopSubStageInfo(tempUserDetailDO.getShopId(), ShopSubStageEnum.SHOP_STAGE_6);
//        if(shopStageInfoDO != null && shopStageInfoDO.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00.getShopSubStageStatus())){
//            shopStageInfoDAO.updateShopStageInfo(tempUserDetailDO.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_60);
//        }

//        List<String> userIdList = enterpriseUserRoleDao.getUserIdsByRoleIdList(Collections.singletonList(UserRoleEnum.TRAINER.getCode()));
//        RegionDO regionDO = regionDao.getRegionById(shopInfoDO.getRegionId());
//        //
//        if (regionDO != null && regionDO.getParentId() != null){
//            userIdList = enterpriseUserDAO.getUserIdListByRegionId(Long.valueOf(regionDO.getParentId()), userIdList);
//
//            Map<String, String> messageMap = new HashMap<>();
//            messageMap.put("userName", tempUserDetailRequest.getName());
//            messageMap.put("mobile", tempUserDetailRequest.getPhone());
//            messageMap.put("registerTime", DateUtils.dateTime(tempUserDetailRequest.getRegisterTime()));
//            messageMap.put("userDetailId", tempUserDetailDO.getId().toString());
//            commonService.sendMessage(userIdList, MessageEnum.MESSAGE_21, messageMap);
//        }
    }

    @Override
    public TempUserDetailInfoVO getUserInfoByIdUserDetailId(Long userDetailId) {
        TempUserDetailDO tempUserDetailDO = tempUserDetailDAO.selectById(userDetailId);
        TempUserDetailInfoVO tempUserDetailInfoVO = new TempUserDetailInfoVO();
        tempUserDetailInfoVO.setLineId(tempUserDetailDO.getLineId());
        tempUserDetailInfoVO.setShopId(tempUserDetailDO.getShopId());
        tempUserDetailInfoVO.setRegionId(tempUserDetailDO.getRegionId());
        tempUserDetailInfoVO.setMobile(tempUserDetailDO.getMobile());
        tempUserDetailInfoVO.setUsername(tempUserDetailDO.getUsername());
        tempUserDetailInfoVO.setSex(tempUserDetailDO.getSex());
        tempUserDetailInfoVO.setAge(tempUserDetailDO.getAge());
        tempUserDetailInfoVO.setIdCard(tempUserDetailDO.getIdCard());
        tempUserDetailInfoVO.setStatus(tempUserDetailDO.getStatus());
        tempUserDetailInfoVO.setEducational(tempUserDetailDO.getEducational());
        tempUserDetailInfoVO.setRoleId(tempUserDetailDO.getRoleId());
        tempUserDetailInfoVO.setIdCardNegativeUrl(tempUserDetailDO.getIdCardNegativeUrl());
        tempUserDetailInfoVO.setIdCardPositiveUrl(tempUserDetailDO.getIdCardPositiveUrl());
        tempUserDetailInfoVO.setHealthCertificateUrl(tempUserDetailDO.getHealthCertificateUrl());
        tempUserDetailInfoVO.setRegisterTime(tempUserDetailDO.getRegisterTime());
        tempUserDetailInfoVO.setTrainTime(tempUserDetailDO.getTrainTime());
        tempUserDetailInfoVO.setCompletionCertificate(tempUserDetailDO.getCompletionCertificate());
        return tempUserDetailInfoVO;
    }

    @Override
    public StaffBaseInfoDTO getUserInfoByIdCard(String idCard) {
        if (StringUtil.isBlank(idCard)) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        TempUserDetailDO tempUserDetailDO = tempUserDetailDAO.selectByIdCard(idCard);
        StaffBaseInfoDTO userInfoByCode = new StaffBaseInfoDTO();
        userInfoByCode.setAge(String.valueOf(tempUserDetailDO.getAge()));
        userInfoByCode.setIdCard(tempUserDetailDO.getIdCard());
        userInfoByCode.setPhone(tempUserDetailDO.getMobile());
        userInfoByCode.setName(tempUserDetailDO.getUsername());
        userInfoByCode.setSex(tempUserDetailDO.getSex());
        userInfoByCode.setStatus(tempUserDetailDO.getStatus());
        userInfoByCode.setIdNumPhoto(tempUserDetailDO.getIdCardPositiveUrl());
        userInfoByCode.setEmblemPhoto(tempUserDetailDO.getIdCardNegativeUrl());
        userInfoByCode.setHighestDegree(tempUserDetailDO.getEducational());
        userInfoByCode.setJobId(tempUserDetailDO.getRoleId());
        userInfoByCode.setEntryDate(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,tempUserDetailDO.getSubmitTime()));
        userInfoByCode.setHealthCertificate(tempUserDetailDO.getHealthCertificateUrl());
        return userInfoByCode;
    }

    @Override
    public void registrationCompleted(Long shopId) {
        ShopStageInfoDO shopStageInfoDO = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_5);
        if(shopStageInfoDO != null){
            shopStageInfoDAO.updateShopStageInfo(shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_51);
            //状态结束
            preparationService.whetherToOpenForAcceptance(shopId);
        }
    }


}
