package com.jeeplus.human.depart.handover.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.human.depart.handover.domain.HandoverAccounts;
import com.jeeplus.human.depart.handover.domain.HandoverRecovery;
import com.jeeplus.human.depart.handover.domain.HandoverSocialSecurity;
import com.jeeplus.human.depart.handover.mapper.HandoverAccountsMapper;
import com.jeeplus.human.depart.handover.mapper.HandoverSocialSecurityMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-11-07 9:33
 */
@Service
@Transactional
public class HandoverSocialSecurityService extends ServiceImpl<HandoverSocialSecurityMapper, HandoverSocialSecurity> {

    @Resource
    private HandoverSocialSecurityMapper socialSecurityMapper;

    @Resource
    private HandoverAccountsMapper accountsMapper;

    @Resource
    private HandoverRecoveryService recoveryService;


    public void deleteByRegistrationId(String id){
        QueryWrapper<HandoverSocialSecurity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("handover_id",id);
        socialSecurityMapper.delete(queryWrapper);
    }

    public HandoverSocialSecurity findById(String id) throws Exception {
        // 查询基础信息表
        HandoverSocialSecurity info = socialSecurityMapper.getById(id);
        return info;
    }

    public void updateStatusById(HandoverSocialSecurity dto) {
        socialSecurityMapper.updateStatusById(dto.getId(), dto.getType());

    }

    public HandoverSocialSecurity saveSocialSecurity(HandoverSocialSecurity accounts) throws Exception {
        if (StringUtils.isNotEmpty(accounts.getId())){

            HandoverSocialSecurity report = socialSecurityMapper.getById(accounts.getId());
            if (report != null){
                accounts.setCreateById(report.getCreateById());
                accounts.setName(report.getName());
                return update(accounts);
            }
        }
        return add(accounts);
    }

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public HandoverSocialSecurity add(HandoverSocialSecurity reportData) throws Exception{

        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        HandoverSocialSecurity report = new HandoverSocialSecurity();
        BeanUtils.copyProperties(reportData, report);
        report.setCreateById(userDTO.getId());
        report.setCreateTime(new Date());
        report.setUpdateTime(new Date());
        report.setUpdateById(userDTO.getId());
        socialSecurityMapper.insert(report);

        return report;
    }
    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public HandoverSocialSecurity update(HandoverSocialSecurity reportData) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        HandoverSocialSecurity report = new HandoverSocialSecurity();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());
        socialSecurityMapper.updateById(report);

        if (report.getType().equals("5")) {
            int i = accountsMapper.selectAllActivityIsComplete(report.getHandoverId());
            if (i != 0) {
                //发起门卡回收流程
                Map map = SpringUtil.getBean(IFlowableApi.class).getByNameForFen("门卡回收流程");
                String procDefId = map.get("id")+"";
                //发起完善餐补卡号流程
                String uid = UUID.randomUUID().toString().replace("-", "");
                HandoverRecovery recovery = new HandoverRecovery();
                recovery.setId(uid);
                recovery.setCreateById("1");
                recovery.setUpdateById("1");
                recovery.setProcessDefinitionId(procDefId);
                recovery.setHandoverId(report.getHandoverId());
                recoveryService.save(recovery);

                String procDefKey = map.get("key")+"";
                String businessTable = "human_resources_depart_handover_recovery";
                String businessId = uid;
                String title = report.getName() + "-离职-门卡回收";
                String assignee = report.getCreateById();
                String recordType = "";
                Map<String,String> newMap = new HashMap();
                newMap.put("procDefId", procDefId);
                newMap.put("procDefKey", procDefKey);
                newMap.put("businessTable", businessTable);
                newMap.put("businessId", businessId);
                newMap.put("title", title);
                newMap.put("assignee", assignee);
                newMap.put("recordType", recordType);
                SpringUtil.getBean(IFlowableApi.class).startForFen(newMap);
                recovery.setType("2");
                recoveryService.saveOrUpdate(recovery);

            }
        }

        return report;
    }
}
