package com.jeeplus.test.cw.contractRegistration.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.cw.contractRegistration.domain.CwWorkContractBorrow;
import com.jeeplus.test.cw.contractRegistration.domain.CwWorkContractBorrowMessage;
import com.jeeplus.test.cw.contractRegistration.mapper.CwWorkContractBorrowMapper;
import com.jeeplus.test.cw.contractRegistration.mapper.CwWorkContractBorrowMessageMapper;
import com.jeeplus.test.cw.contractRegistration.service.dto.CwWorkContractBorrowDto;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.Date;
/**
 * @author: 王强
 * @create: 2022-11-27 13:51
 **/
@Service
public class CwWorkContractBorrowService {

    @Resource
    private CwWorkContractBorrowMapper borrowMapper;

    @Resource
    private CwWorkContractBorrowMessageMapper messageMapper;

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

    /**
     * 合同登记借用新增
     */
    @Transactional(rollbackFor = Exception.class)
    public String add(CwWorkContractBorrowDto workContractBorrowDto) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        String id = UUID.randomUUID().toString().replace("-", "");
        CwWorkContractBorrow borrow = new CwWorkContractBorrow();
        BeanUtils.copyProperties(workContractBorrowDto, borrow);
        borrow.setId(id);
        borrow.setCreateBy(userDTO.getId());
        borrow.setCreateDate(new Date());
        borrow.setUpdateBy(userDTO.getId());
        borrow.setUpdateDate(new Date());
        borrowMapper.insert(borrow);
        //借用记录信息
        this.saveMessage(workContractBorrowDto, id);
        return id;
    }

    /**
     * 合同登记修改
     */
    @Transactional(rollbackFor = Exception.class)
    public String update(CwWorkContractBorrowDto workContractBorrowDto, String id) {
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        CwWorkContractBorrow borrow = new CwWorkContractBorrow();
        BeanUtils.copyProperties(workContractBorrowDto, borrow);
        borrow.setContractInfoId(id);
        borrow.setUpdateBy(userDTO.getId());
        borrow.setUpdateDate(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) {
        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 = UserUtils.getCurrentUserDTO();
        CwWorkContractBorrowMessage message = new CwWorkContractBorrowMessage();
        BeanUtils.copyProperties(workContractBorrowDto, message);
        String messageId = UUID.randomUUID().toString().replace("-", "");
        message.setId(messageId);
        message.setCreateBy(userDTO.getId());
        message.setCreateDate(new Date());
        message.setUpdateBy(userDTO.getId());
        message.setUpdateDate(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;
    }
}
