package com.jeeplus.assess.workContract.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.assess.workContract.domain.WorkContractBorrow;
import com.jeeplus.assess.workContract.domain.WorkContractBorrowMessage;
import com.jeeplus.assess.workContract.mapper.WorkContractBorrowMapper;
import com.jeeplus.assess.workContract.mapper.WorkContractBorrowMessageMapper;
import com.jeeplus.assess.workContract.service.dto.WorkContractBorrowDto;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.UserUtils;
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.List;
import java.util.UUID;

@Service
public class WorkContractBorrowService {

    @Resource
    private WorkContractBorrowMapper workContractBorrowMapper;

    @Resource
    private WorkContractBorrowMessageMapper workContractBorrowMessageMapper;

    public String saveInfo(WorkContractBorrowDto workContractBorrowDto) throws Exception {
        if (StringUtils.isNotEmpty(workContractBorrowDto.getId())) {
            WorkContractBorrow info = workContractBorrowMapper.selectById( workContractBorrowDto.getId());
            if (info != null) {
                return update(workContractBorrowDto, info.getContractInfoId());
            }
        }
        return add(workContractBorrowDto);
    }

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

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

    /**
     * 根据id修改status
     */
    public void updateStatusById(WorkContractBorrowDto workContractBorrowDto) {
        workContractBorrowMapper.updateStatusById(workContractBorrowDto.getId(), workContractBorrowDto.getBorrowType());
    }

    public void updateStatusByContractInfoId(WorkContractBorrowDto workContractBorrowDto) {
        workContractBorrowMapper.updateStatusByContractInfoId(workContractBorrowDto.getId(), workContractBorrowDto.getBorrowType());
    }

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

    public WorkContractBorrow findByContractInfoId(String id) {
        return workContractBorrowMapper.selectFileByContractInfoId(id);
    }

    /**
     * 借用记录保存
     * @param workContractBorrowDto
     * @param id
     * @return
     */
    public void saveMessage (WorkContractBorrowDto workContractBorrowDto, String id) {
        //保存借用信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        WorkContractBorrowMessage message = new WorkContractBorrowMessage();
        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");
        workContractBorrowMessageMapper.insert(message);
    }

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

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

    public List<WorkContractBorrowMessage> findMessageList(String id) {
        WorkContractBorrow borrow = findByContractInfoId(id);
        if (borrow != null) {
            return workContractBorrowMapper.findMessageList(borrow.getId());
        }
        return null;
    }
}
