package com.jeeplus.finance.contractRegistration.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.finance.contractRegistration.domain.CwWorkContractBorrow;
import com.jeeplus.finance.contractRegistration.domain.CwWorkContractBorrowMessage;
import com.jeeplus.finance.contractRegistration.mapper.ContractInfoMapper;
import com.jeeplus.finance.contractRegistration.mapper.CwWorkContractBorrowMapper;
import com.jeeplus.finance.contractRegistration.mapper.CwWorkContractBorrowMessageMapper;
import com.jeeplus.finance.contractRegistration.service.dto.CwWorkContractBorrowDto;
//先注释import com.jeeplus.flowable.model.MyNoticeList;
//import com.jeeplus.flowable.service.MyNoticeService;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.sys.feign.IDictApi;
import com.jeeplus.sys.feign.ITenantApi;
import com.jeeplus.sys.feign.IUserApi;
//import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.DictUtils;
//import com.jeeplus.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.FlowNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author: 王强
 * @create: 2022-11-27 13:51
 **/
@Service
public class CwWorkContractBorrowService {

    @Resource
    private CwWorkContractBorrowMapper borrowMapper;

    @Resource
    private CwWorkContractBorrowMessageMapper messageMapper;

    @Resource
    private ContractInfoMapper infoMapper;

//先注释    @Resource
//    private MyNoticeService myNoticeService;

    @Resource
    private IFlowableApi flowTaskService;

//    @Resource
//    private UserService userService;

    public String saveInfo(CwWorkContractBorrowDto workContractBorrowDto) throws Exception {
        if (StringUtils.isNotEmpty(workContractBorrowDto.getId())) {
            CwWorkContractBorrow info = borrowMapper.selectById( workContractBorrowDto.getId());
            if (info != null) {
                infoMapper.updateBorrowTypeById(info.getContractInfoId(),workContractBorrowDto.getBorrowType());
                return update(workContractBorrowDto, info.getContractInfoId());
            }
        }
        return add(workContractBorrowDto);
    }

    /**
     * 合同登记借用新增
     */
    @Transactional(rollbackFor = Exception.class)
    public String add(CwWorkContractBorrowDto workContractBorrowDto) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        String id = UUID.randomUUID().toString().replace("-", "");
        CwWorkContractBorrow borrow = new CwWorkContractBorrow();
        BeanUtils.copyProperties(workContractBorrowDto, borrow);
        borrow.setId(id);
        borrow.setCreateById(userDTO.getId());
        borrow.setCreateTime(new Date());
        borrow.setUpdateById(userDTO.getId());
        borrow.setUpdateTime(new Date());
        borrowMapper.insert(borrow);
        //借用记录信息
        this.saveMessage(workContractBorrowDto, id);
        return id;
    }

    /**
     * 合同登记修改
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(CwWorkContractBorrowDto workContractBorrowDto, String id) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        CwWorkContractBorrow borrow = new CwWorkContractBorrow();
        BeanUtils.copyProperties(workContractBorrowDto, borrow);
        borrow.setContractInfoId(id);
        borrow.setUpdateById(userDTO.getId());
        borrow.setUpdateTime(new Date());
        borrowMapper.updateById(borrow);
        //借用记录信息
        this.saveMessage(workContractBorrowDto, workContractBorrowDto.getId());
        return borrow.getId();
    }
    /**
     * 根据id修改status
     */
    public void updateStatusById(CwWorkContractBorrowDto workContractBorrowDto) {
        borrowMapper.updateStatusById(workContractBorrowDto.getId(), workContractBorrowDto.getBorrowType());
    }

    public void updateStatusByContractInfoId(CwWorkContractBorrowDto workContractBorrowDto) {
        infoMapper.updateBorrowTypeById(workContractBorrowDto.getId(),workContractBorrowDto.getBorrowType());
        borrowMapper.updateStatusByContractInfoId(workContractBorrowDto.getId(), workContractBorrowDto.getBorrowType());
    }

    public CwWorkContractBorrow findById(String id) {
        CwWorkContractBorrow borrow = borrowMapper.selectById(id);
        if (borrow == null) {
            LambdaQueryWrapper<CwWorkContractBorrow> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CwWorkContractBorrow::getContractInfoId, id);
            return borrowMapper.selectOne(wrapper);
        }
        return borrow;
    }

    public CwWorkContractBorrow findByContractInfoId(String id) {
        return borrowMapper.selectFileByContractInfoId(id);
    }

    /**
     * 借用记录保存
     * @param workContractBorrowDto
     * @param id
     * @return
     */
    public void saveMessage (CwWorkContractBorrowDto workContractBorrowDto, String id) {
        //保存借用信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        CwWorkContractBorrowMessage message = new CwWorkContractBorrowMessage();
        BeanUtils.copyProperties(workContractBorrowDto, message);
        String messageId = UUID.randomUUID().toString().replace("-", "");
        message.setId(messageId);
        message.setCreateById(userDTO.getId());
        message.setCreateTime(new Date());
        message.setUpdateById(userDTO.getId());
        message.setUpdateTime(new Date());
        message.setContractBorrowId(id);
//        message.setBorrowType("3");
        message.setBorrowType(workContractBorrowDto.getBorrowType());
//        message.setRetData(workContractBorrowDto.getBorrowRetData());
        messageMapper.insert(message);
    }

    /**
     * 根据contractBorrowId修改borrowType
     */
    public void updateMessageByBorrowId(CwWorkContractBorrowDto workContractBorrowDto, String type) {
        if ("reture".equals(type)) {
            CwWorkContractBorrow borrow = borrowMapper.selectFileByContractInfoId(workContractBorrowDto.getId());
            if (borrow != null) {
                borrowMapper.updateMessageByBorrowId(borrow.getId(), workContractBorrowDto.getBorrowType(), type);
            }
        } else {
            borrowMapper.updateMessageByBorrowId(workContractBorrowDto.getId(), workContractBorrowDto.getBorrowType(), null);
        }
    }

    public void deleteById(String id) {
        LambdaQueryWrapper<CwWorkContractBorrow> wrapper = new LambdaQueryWrapper<CwWorkContractBorrow>();
        wrapper.eq(CwWorkContractBorrow::getContractInfoId, id);
        CwWorkContractBorrow borrow = borrowMapper.selectOne(wrapper);
        if (borrow != null) {
            borrowMapper.deleteMessageById(borrow.getId());
        }
    }

    public List<CwWorkContractBorrowMessage> findMessageList(String id) {
        CwWorkContractBorrow borrow = findByContractInfoId(id);
        if (borrow != null) {
            return borrowMapper.findMessageList(borrow.getId());
        }
        return null;
    }

    /**
     * 查询临期合同借用信息
     * @return
     */
    public List<CwWorkContractBorrowMessage> getAdventMessageList() {
        String adventDay = SpringUtil.getBean ( IDictApi.class ).getDictLabel("advent_day", "cw_borrow_advent", "");
//        String adventDay = DictUtils.getDictLabel("advent_day", "cw_borrow_advent", "");
        adventDay = "-" + adventDay;
        List<CwWorkContractBorrowMessage> adventMessageList = borrowMapper.getAdventMessageList(adventDay);
        if(null != adventMessageList && adventMessageList.size()>0){
            for (CwWorkContractBorrowMessage info : adventMessageList) {
                String taskName = null;
                String titleStr = null;

                //如果当前时间大于过期时间
                //Date类的一个方法，如果info.getAdventDateDate()早于 new Date() 返回true，否则返回false
                if(info.getBorrowRetData().before(new Date())){
                    taskName = "合同【"+ info.getContractName()+"】借用已超期";
                    titleStr = "合同【"+ info.getContractName()+"】借用已超期。超期时间："+dateToString(info.getBorrowRetData()) +"。";
                }else{
                    taskName = "合同【"+ info.getContractName()+"】即将借用超期";
                    titleStr = "合同【"+ info.getContractName()+"】即将借用超期。超期时间："+dateToString(info.getBorrowRetData());
                }


                //计算两日期之间的天数
                Integer interval = Integer.valueOf(this.getInterval(info.getBorrowRetData(), new Date()));
                if(interval > 0){
                    titleStr = titleStr + "已超期：" + interval + "天。请尽快归还";
                }

                Set<String> noticeUserSet = new HashSet<String>();
                if(StringUtils.isNotBlank(info.getBorrowName())){
                    String tenantId = SpringUtil.getBean ( ITenantApi.class ).getCurrentTenantId ( );
                    UserDTO borrowUser = SpringUtil.getBean ( IUserApi.class ).getByLoginName(info.getBorrowName(),tenantId);
                    if(null != borrowUser && StringUtils.isNotBlank(borrowUser.getId())){
                        noticeUserSet.add(borrowUser.getId());
                    }
                }
                if(StringUtils.isNotBlank(info.getContractCreateById())){
                    noticeUserSet.add(info.getContractCreateById());
                }
//先注释                List<String> noticeUserList = new ArrayList<String>(noticeUserSet);
//                if(noticeUserList.size()>0){
//                    MyNoticeList myNotice = new MyNoticeList();
//                    myNotice.setCreateById("1");
//                    myNotice.setCreateTime(new Date());
//                    myNotice.setUpdateById("1");
//                    myNotice.setUpdateTime(new Date());
//                    myNotice.setDelFlag(0);
//                    //根据procInsId获取taskId
//                    String currentTask = flowTaskService.getTaskIdByprocInstId(info.getProcInsId());
//                    myNotice.setTaskId(currentTask);
//                    myNotice.setDefId(info.getProcessDefinitionId());
//                    myNotice.setTitle(titleStr);
//                    myNotice.setTaskName(taskName);
//                    myNotice.setLink("结束");
//                    myNotice.setType("0");
//                    myNotice.setCreateUser("管理员");
//                    myNotice.setCreateTime(new Date());
//
//                    for (String noticeUserId : noticeUserList) {
//                        myNotice.setNoticeId(noticeUserId);
//                        myNotice.setNoticeName(UserUtils.get(noticeUserId).getName());
//                        //根据taskName和通知人 查询重复数量
//                        MyNoticeList repetitionCountBymyNotice = myNoticeService.getRepetitionCountBymyNotice(myNotice);
//                        if(null == repetitionCountBymyNotice){
//                            // 生成id
//                            String id = UUID.randomUUID().toString().replace("-", "");
//                            myNotice.setId(id);
//                            myNotice.setRepetitionCount(0);
//                            myNoticeService.insertMyNotice(myNotice);
//                        }else{
//                            myNotice.setId(repetitionCountBymyNotice.getId());
//                            myNotice.setRepetitionCount(repetitionCountBymyNotice.getRepetitionCount()+1);
//                            myNoticeService.updateMyNotice(myNotice);
//                        }
//                    }
//
//                }
            }
        }
        return null;
    }


    /**
     * 计算两日期相差天数
     * @param beginDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public String getInterval(Date beginDate, Date endDate){
        long day = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            if(beginDate != null){
                String begin = sdf.format(beginDate);
                beginDate  = sdf.parse(begin);
            }
            if(endDate!= null){
                String end= sdf.format(endDate);
                endDate= sdf.parse(end);
            }
        } catch (Exception e){
            e.getMessage();
        }
        day = (endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
        return String.valueOf(day);
    }
    public static String dateToString(Date date) {
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");//日期格式

        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        // SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小时制

        String s = sformat.format(date);

        return s;
    }

}
