package cn.com.ty.lift.business.capital.service;

import cn.com.ty.lift.business.capital.dao.entity.CapitalRepair;
import cn.com.ty.lift.business.capital.dao.entity.CapitalRepairHistory;
import cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest;
import cn.com.ty.lift.business.capital.dao.entity.model.response.CapitalRepairHistoryResponse;
import cn.com.ty.lift.business.capital.dao.mapper.CapitalRepairHistoryMapper;
import cn.com.ty.lift.business.common.BeanUtils;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * @author bieao
 * @date 2020/4/25 15:21 下午
 * @description 大修项目操作记录业务层
 */
@Service
@Slf4j
public class CapitalRepairHistoryService extends ServiceImpl<CapitalRepairHistoryMapper, CapitalRepairHistory> {

    @Resource
    private BeanUtils utils;

    /**
     * @param request 大修项目操作记录查询条件
     * @return IPage<CapitalRepairHistoryResponse> 结果集
     * @description 查询大修项目操作记录列表
     * @date 2020/4/28 9:57 上午
     */
    public IPage<CapitalRepairHistoryResponse> historyList(CapitalRepairRequest request) {
        return baseMapper.queryCapitalRepairHistory(request.getPage(), request.getProjectId());
    }

    /**
     * @param oldEntry 原有大修项目
     * @param newEntry 修改后的大修项目
     * @return
     * @description 保存大修项目操作记录
     * @date 2020/4/28 9:52 上午
     */
    public CapitalRepairHistory saveOperaHistory(CapitalRepair oldEntry, CapitalRepair newEntry, Long mtCompanyId) {
        Map<String, String> resultMap = new HashMap<>();
        try {
            resultMap = utils.compareAttribute(oldEntry, newEntry);
            if (MapUtil.isEmpty(resultMap)) return null;
        } catch (NoSuchFieldException e) {
            log.error("大修项目操作记录保存异常", e);
        }
        CapitalRepairHistory capitalRepairHistory = new CapitalRepairHistory();
        capitalRepairHistory.setProjectId(oldEntry.getId());
        capitalRepairHistory.setCode(newEntry.getProjectCode());
        capitalRepairHistory.setName(newEntry.getProjectName());
        capitalRepairHistory.setMtCompanyId(mtCompanyId);
        capitalRepairHistory.setDescription("编辑大修信息");
        capitalRepairHistory.setBeforeContent(resultMap.get("before"));
        capitalRepairHistory.setAfterContent(resultMap.get("after"));
        capitalRepairHistory.setOperatorId(10000L);
        capitalRepairHistory.setOperateDate(LocalDateTime.now());
        return capitalRepairHistory;
    }

}
