package com.ruoyi.ems.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ems.domain.MeterDevice;
import com.ruoyi.ems.domain.MeterReading;
import com.ruoyi.ems.domain.vo.QueryMeterReading;
import com.ruoyi.ems.service.IMeterDeviceService;
import com.ruoyi.ems.service.IMeterReadingService;
import com.ruoyi.ems.util.DateUtils;
import com.ruoyi.ems.util.exception.Assert;
import com.ruoyi.ems.util.exception.BusinessException;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 抄记录Controller
 *
 * @author ruoyi
 * @date 2024-08-28
 */
@RestController
@RequestMapping("/meterReading")
@Api(value = "MeterReadingManualController", description = "抄表管理")
public class MeterReadingController extends BaseController {
    @Autowired
    private IMeterReadingService meterReadingService;

    @Autowired
    private IMeterDeviceService meterDeviceService;

    /**
     * 查询抄表记录列表
     */
    @GetMapping("/listByParam")
    public AjaxResult listByParam(QueryMeterReading queryMeterReading) {
        List<MeterReading> list = meterReadingService.selectList(queryMeterReading);
        return success(list);
    }

    /**
     * 获取设备最后一条抄表记录
     */
    @GetMapping(value = "/getLastRecord")
    public AjaxResult getLastRecord(@RequestParam(name = "areaCode") String areaCode,
        @RequestParam(name = "deviceCode") String deviceCode) {
        return success(meterReadingService.selectLastItem(areaCode, deviceCode));
    }

    /**
     * 新增抄表记录
     */
    @RequiresPermissions("ems:meterReading:add")
    @Log(title = "抄表记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MeterReading meterRec) {
        try {
            fillData(meterRec);
            int count = meterReadingService.insert(meterRec);
            return toAjax(count);
        }
        catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改抄表记录
     */
    @RequiresPermissions("ems:meterReading:edit")
    @Log(title = "抄表记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MeterReading meterRec) {
        try {
            MeterReading dbItem = meterReadingService.selectById(meterRec.getId());
            Assert.notNull(dbItem, -1, "该记录不存在");
            meterReadingService.deleteById(meterRec.getId());
            dbItem.setMeterReading(meterRec.getMeterReading());
            fillData(meterRec);
            int count = meterReadingService.insert(meterRec);
            return toAjax(count);
        }
        catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除动力箱柜
     */
    @RequiresPermissions("ems:meterReading:remove")
    @Log(title = "抄表记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(meterReadingService.deleteById(id));
    }

    private void fillData(MeterReading meterRec) {
        MeterReading lastItem = meterReadingService.selectLastItem(meterRec.getAreaCode(),
            meterRec.getDeviceCode());
        MeterDevice meterDevice = meterDeviceService.selectMeterDeviceByCode(meterRec.getAreaCode(),
            meterRec.getDeviceCode());
        Date date = new Date();

        if (null != lastItem) {
            Assert.isTrue(meterRec.getMeterReading() >= lastItem.getMeterReading(), -1, "抄表值不能小于上一条记录");
            meterRec.setYear(DateUtils.dateToString(date, "yyyy"));
            meterRec.setMeterMonth(DateUtils.dateToString(date, "yyyyMM"));
            meterRec.setLastReading(lastItem.getMeterReading());
            meterRec.setLastTime(lastItem.getMeterTime());
            meterRec.setMeterTime(date);
            meterRec.setIncrease(
                (meterRec.getMeterReading() - lastItem.getMeterReading()) * meterDevice.getMagnification());
        }
        else {
            meterRec.setYear(DateUtils.dateToString(date, "yyyy"));
            meterRec.setMeterMonth(DateUtils.dateToString(date, "yyyyMM"));
            meterRec.setMeterTime(date);
            meterRec.setIncrease(meterRec.getMeterReading() * meterDevice.getMagnification());
        }
    }
}
