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

import cn.com.ty.lift.business.common.BeanUtils;
import cn.com.ty.lift.business.library.dao.entity.Lift;
import cn.com.ty.lift.business.library.dao.entity.LiftHistory;
import cn.com.ty.lift.business.library.dao.entity.model.request.LiftRequest;
import cn.com.ty.lift.business.library.dao.entity.model.response.LiftHistoryResponse;
import cn.com.ty.lift.business.library.dao.mapper.LiftHistoryMapper;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;

/**
 * <p>
 * 电梯操作记录 服务实现类
 * </p>
 *
 * @author wcz
 * @since 2020-01-07
 */
@Service
@Slf4j
public class LiftHistoryService extends ServiceImpl<LiftHistoryMapper, LiftHistory> {

    @Resource
    private BeanUtils utils;

    /**
     * @param request 电梯操作记录查询条件
     * @return IPage<ProjectHistory> 结果集
     * @description 查询电梯操作记录列表
     * @date 2020/4/27 2:19 下午
     */
    public IPage<LiftHistoryResponse> historyList(LiftRequest request) {
      return baseMapper.queryLiftHistory(request.getPage(),request);
    }

    /**
     * @param oldEntry 原有电梯
     * @param newEntry 修改后的电梯
     * @return
     * @description 保存电梯操作记录
     * @date 2020/4/28 9:52 上午
     */
    public LiftHistory createOperaHistory(Lift oldEntry, Lift 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);
        }
        LiftHistory liftHistory = new LiftHistory();
        liftHistory.setLiftId(oldEntry.getId());
        liftHistory.setMtCompanyId(mtCompanyId);
        liftHistory.setDescription("编辑电梯信息");
        liftHistory.setCode(newEntry.getLiftCode());
        liftHistory.setBeforeContent(resultMap.get("before"));
        liftHistory.setAfterContent(resultMap.get("after"));
        liftHistory.setOperatorId(10000L);
        liftHistory.setOperateDate(LocalDateTime.now());
        return liftHistory;
    }
}
