package com.tofly.feesapi.yhgl.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.feesapi.bjgl.entity.YhCbkxx;
import com.tofly.feesapi.bjgl.entity.YhCbkxxLog;
import com.tofly.feesapi.bjgl.mapper.YhCbkxxLogMapper;
import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.core.dto.Order;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.common.util.ToolsUtils;
import com.tofly.feesapi.yhgl.entity.DaCustomerAndMeter;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.entity.YhKhjbxxLog;
import com.tofly.feesapi.yhgl.entity.dto.YhywLogFilter;
import com.tofly.feesapi.yhgl.service.YhKhjbxxLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.yhgl.mapper.YhKhjbxxLogMapper;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户档案日志记录表
 */
@Service("yhKhjbxxLogService")
public class YhKhjbxxLogServiceImpl extends ServiceImpl<YhKhjbxxLogMapper, YhKhjbxxLog> implements YhKhjbxxLogService {

    @Autowired
    private YhCbkxxLogMapper yhCbkxxLogMapper;

    @Override
    public PageResult<YhKhjbxxLog> getLogPage(PageDto pageDto, YhywLogFilter filter) {
        List<Order> orders = pageDto.getOrders();
        orders = orders.stream().map(item -> {
            if (item.getColumn().equals("yhbh")) {
                Order order = new Order();
                order.setColumn("a.yhbh");
                order.setAsc(item.isAsc());
                return order;
            }
            return item;
        }).collect(Collectors.toList());
        pageDto.setOrders(orders);

        PageDto<YhKhjbxxLog> page = baseMapper.getLogPage(pageDto, filter);
        List<YhKhjbxxLog> records = page.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            for (YhKhjbxxLog record : records) {
                String newJson = record.getNewJson();
                String oldJson = record.getOldJson();

                if (StrUtil.isNotEmpty(oldJson) && StrUtil.isNotEmpty(newJson)) {
                    YhKhjbxx yhKhjbxx1 = JSONUtil.toBean(oldJson, YhKhjbxx.class);
                    YhKhjbxx yhKhjbxx2 = JSONUtil.toBean(newJson, YhKhjbxx.class);
                    record.setDifferentFields(ToolsUtils.compareObjects(yhKhjbxx1, yhKhjbxx2, false));
                }
            }
        }
        PageResult<YhKhjbxxLog> pageResult = page.toVo();
        return pageResult;
    }

    @Override
    public ResultRespone<Object> addDaCustomerAndMeterLog(DaCustomerAndMeter daCustomerAndMeter, String operationType) throws Exception {
        ResultRespone<Object> rest = new ResultRespone<>();
        YhKhjbxx yhKhjbxx = daCustomerAndMeter.getYhKhjbxx();
        YhKhjbxxLog daCustomerLog = new YhKhjbxxLog();
        BeanUtils.copyProperties(yhKhjbxx, daCustomerLog);
        daCustomerLog.setLogCzsj(new Date());
        daCustomerLog.setCzsj(new Date());
        daCustomerLog.setLogCzryxm(yhKhjbxx.getCzryxm());
        daCustomerLog.setLogCzrybm(yhKhjbxx.getCzrybm());
        daCustomerLog.setCzryxm(yhKhjbxx.getCzryxm());
        daCustomerLog.setCzrybm(yhKhjbxx.getCzrybm());
        daCustomerLog.setLogId(GuidGenerator.getGuid());

        if (operationType.equals("save")) {
            daCustomerLog.setLogCzlx("添加");
            daCustomerLog.setLogCllx("添加");
        } else {
            daCustomerLog.setLogCzlx("修改");
            daCustomerLog.setLogCllx("修改");
        }

        //水表档案操作日志记录
        YhCbkxxLog daMeterLog = new YhCbkxxLog();
        YhCbkxx yhCbkxx = daCustomerAndMeter.getYhCbkxx();
        BeanUtils.copyProperties(yhCbkxx, daMeterLog);
        daMeterLog.setLogId(GuidGenerator.getGuid());
        daMeterLog.setLogLsh("");
        daMeterLog.setLogCzsj(new Date());
        daMeterLog.setLogCzryxm(yhCbkxx.getCzryxm());
        daMeterLog.setLogCzrybm(yhCbkxx.getCzrybm());
        daMeterLog.setBz("");

        if (operationType.equals("save")) {
            daMeterLog.setLogCzlx("添加");
            daMeterLog.setLogCllx("添加");
        } else {
            daMeterLog.setLogCzlx("修改");
            daMeterLog.setLogCllx("修改");
        }

        baseMapper.insert(daCustomerLog);
        yhCbkxxLogMapper.insert(daMeterLog);

        return rest;
    }
}
