package com.ruoyi.ems.controller;

import com.huashe.common.domain.AjaxResult;
import com.huashe.common.exception.Assert;
import com.huashe.common.exception.BusinessException;
import com.huashe.common.utils.DateUtils;
import com.ruoyi.common.core.web.controller.BaseController;
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.model.QueryMeter;
import com.ruoyi.ems.service.IMeterDeviceService;
import com.ruoyi.ems.service.IMeterReadingService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
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(QueryMeter 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 nowItem) {
        try {
            MeterDevice meterDevice = meterDeviceService.selectMeterDeviceByCode(nowItem.getDeviceCode());
            Assert.notNull(meterDevice, -1, "设备不存在");
            nowItem.setAreaCode(meterDevice.getAreaCode());
            MeterReading lastItem = meterReadingService.selectLastItem(meterDevice.getAreaCode(), nowItem.getDeviceCode());
            checkAdd(lastItem, nowItem);
            fillData(lastItem, nowItem);
            int count = meterReadingService.insert(nowItem);
            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 nowItem) {
        try {
            MeterReading lastItem = meterReadingService.selectById(nowItem.getId());
            Assert.notNull(lastItem, -1, "记录不存在");
            nowItem.setMeterReading(nowItem.getMeterReading());
            int count = meterReadingService.update(nowItem);
            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 checkAdd(MeterReading lastItem, MeterReading nowItem) {
        if (null != lastItem) {
            Assert.isTrue(!StringUtils.equals(nowItem.getMeterMonth(), lastItem.getMeterMonth()), -1, "本月已存在抄表记录");
            Assert.isTrue(nowItem.getMeterReading() >= lastItem.getMeterReading(), -1, "抄表值不能小于上一条记录");
        }
    }

    private void fillData(MeterReading lastItem, MeterReading meterRec) {
        MeterDevice meterDevice = meterDeviceService.selectMeterDeviceByCode(meterRec.getDeviceCode());

        if (null != lastItem) {
            meterRec.setYear(StringUtils.substring(lastItem.getYear(), 0, 4));
            meterRec.setLastReading(lastItem.getMeterReading());
            meterRec.setLastTime(lastItem.getMeterTime());
            meterRec.setIncrease(
                (meterRec.getMeterReading() - lastItem.getMeterReading()) * meterDevice.getMagnification());
        }
        else {
            meterRec.setIncrease(meterRec.getMeterReading() * meterDevice.getMagnification());
        }
    }
}
