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.HandoverImprest;
import com.jeeplus.human.depart.handover.domain.HandoverRecovery;
import com.jeeplus.human.depart.handover.mapper.HandoverAccountsMapper;
import com.jeeplus.human.depart.handover.mapper.HandoverImprestMapper;
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-14 9:10
 */
@Service
@Transactional
public class HandoverImprestService extends ServiceImpl<HandoverImprestMapper, HandoverImprest> {

    @Resource
    private HandoverImprestMapper imprestMapper;

    @Resource
    private HandoverAccountsMapper accountsMapper;

    @Resource
    private HandoverRecoveryService recoveryService;

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

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

    public void updateStatusById(HandoverImprest dto) {
        imprestMapper.updateStatusById(dto.getId(), dto.getType());

    }

    public HandoverImprest saveImprest(HandoverImprest accounts) throws Exception {
        if (StringUtils.isNotEmpty(accounts.getId())){

            HandoverImprest report = imprestMapper.getById(accounts.getId());
            if (report != null){
                accounts.setCreateById(report.getCreateById());
                return update(accounts);
            }
        }
        return add(accounts);
    }

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

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

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

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

        HandoverImprest report = new HandoverImprest();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());
        imprestMapper.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 = "门卡回收";
                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;
    }
}
