package com.xjrsoft.module.personnel.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.mqtt.EmqClient;
import com.xjrsoft.module.personnel.entity.CarManagement;
import com.xjrsoft.module.personnel.mapper.CarManagementMapper;
import com.xjrsoft.module.personnel.service.ICarManagementService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
@Slf4j
public class CarManagementServiceImpl extends MPJBaseServiceImpl<CarManagementMapper, CarManagement> implements ICarManagementService {
    private final CarManagementMapper carManagementMapper;

    private EmqClient emqClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CarManagement carManagement) {
        LambdaQueryWrapper<CarManagement> queryWrapper = new LambdaQueryWrapper<CarManagement>().eq(CarManagement::getCarNo, carManagement.getCarNo());
//        queryWrapper.eq(CarManagement::getCarNo, carManagement.getCarNo());
        CarManagement existCar = carManagementMapper.selectOne(queryWrapper);
        if (existCar != null) {
            throw new MyException(String.format("车牌【%s】已存在！", carManagement.getCarNo()));
        }

        carManagementMapper.insert(carManagement);

        // 推送数据客户端
        JSONObject jsonObject = JSONUtil.createObj()
                .put("carNo", carManagement.getCarNo())
                .put("cName", carManagement.getCName())
                .put("cStartTime", DateUtil.format(carManagement.getCStartTime(), "yyyy-MM-dd HH:mm:ss"))
                .put("cEndTime", DateUtil.format(carManagement.getCEndTime(), "yyyy-MM-dd HH:mm:ss"))
                .put("cType", carManagement.getCType())
                .put("cFeeType", carManagement.getCFeeType());

        try {
            emqClient.publish("/car/identify/add", jsonObject);
        } catch (MqttException e) {
            log.error("mqtt", "publish", "/car/identify/add", e.getMessage());
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(CarManagement carManagement) {
        LambdaQueryWrapper<CarManagement> queryWrapper = new LambdaQueryWrapper<CarManagement>().eq(CarManagement::getId, carManagement.getId());
//        queryWrapper.eq(CarManagement::getId, carManagement.getId());
        CarManagement oldCar = carManagementMapper.selectOne(queryWrapper);
        // 更新数据
        carManagementMapper.updateById(carManagement);

        // 推送数据客户端
        JSONObject jsonObject = JSONUtil.createObj()
                .put("cId", oldCar.getCSyncId())
                .put("carNo", carManagement.getCarNo())
                .put("cName", carManagement.getCName())
                .put("cStartTime", DateUtil.format(carManagement.getCStartTime(), "yyyy-MM-dd HH:mm:ss"))
                .put("cEndTime", DateUtil.format(carManagement.getCEndTime(), "yyyy-MM-dd HH:mm:ss"))
                .put("cType", carManagement.getCType())
                .put("cFeeType", carManagement.getCFeeType());

        try {
            emqClient.publish("/car/identify/update", jsonObject);
        } catch (MqttException e) {
            log.error("mqtt", "publish", "/car/identify/update", e.getMessage());
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        LambdaQueryWrapper<CarManagement> queryWrapper = new LambdaQueryWrapper<CarManagement>().in(CarManagement::getId,ids);
        List<CarManagement> oldCars = carManagementMapper.selectList(queryWrapper);
        // 删除数据
        carManagementMapper.deleteBatchIds(ids);

        for (CarManagement item : oldCars) {
            // 推送数据客户端
            JSONObject jsonObject = JSONUtil.createObj()
                    .put("cId", item.getCSyncId());

            try {
                emqClient.publish("/car/identify/delete", jsonObject);
            } catch (MqttException e) {
                log.error("mqtt", "publish", "/car/identify/delete", e.getMessage());
            }
        }

        return true;
    }
}
