package com.jjt.elec.controller;

import com.jjt.common.annotation.Log;
import com.jjt.common.core.controller.BaseController;
import com.jjt.common.core.domain.AjaxResult;
import com.jjt.common.core.page.TableDataInfo;
import com.jjt.common.enums.BusinessType;
import com.jjt.common.utils.poi.ExcelUtil;
import com.jjt.elec.domain.ElecPrice;
import com.jjt.elec.service.IElecPriceService;
import com.jjt.ws.service.ITwinWorkshopCalcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;

/**
 * 电费价格配置Controller
 *
 * @author wukai
 * @date 2025-03-14
 */
@Api(tags = "电费价格配置")
@RestController
@RequestMapping("/elec/price")
public class ElecPriceController extends BaseController {
    @Resource
    private IElecPriceService elecPriceService;
    @Resource
    private ITwinWorkshopCalcService workshopCalcService;

    /**
     * 查询电费价格配置列表
     */
    @ApiOperation("查询电费价格配置列表")
    //@PreAuthorize("@ss.hasPermi('elec:price:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElecPrice elecPrice) {
        startPage();
        List<ElecPrice> list = elecPriceService.selectElecPriceList(elecPrice);
        return getDataTable(list);
    }

    /**
     * 导出电费价格配置列表
     */
    @ApiOperation("导出电费价格配置列表")
    //@PreAuthorize("@ss.hasPermi('elec:price:export')")
    @Log(title = "电费价格配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElecPrice elecPrice) {
        List<ElecPrice> list = elecPriceService.selectElecPriceList(elecPrice);
        ExcelUtil<ElecPrice> util = new ExcelUtil<ElecPrice>(ElecPrice.class);
        util.exportExcel(response, list, "电费价格配置数据");
    }

    /**
     * 获取电费价格配置详细信息
     */
    @ApiOperation("获取电费价格配置详细信息")
    //@PreAuthorize("@ss.hasPermi('elec:price:query')")
    @GetMapping(value = "/{priceId}")
    public AjaxResult getInfo(@PathVariable("priceId") Long priceId) {
        return success(elecPriceService.selectElecPriceByPriceId(priceId));
    }

    /**
     * 新增电费价格配置
     */
    @ApiOperation("新增电费价格配置")
    //@PreAuthorize("@ss.hasPermi('elec:price:add')")
    @Log(title = "电费价格配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElecPrice elecPrice) {
        return toAjax(elecPriceService.insertElecPrice(elecPrice));
    }

    /**
     * 修改电费价格配置
     */
    @ApiOperation("修改电费价格配置")
    //@PreAuthorize("@ss.hasPermi('elec:price:edit')")
    @Log(title = "电费价格配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElecPrice elecPrice) {
        return toAjax(elecPriceService.updateElecPrice(elecPrice));
    }

    @ApiOperation("重新统计")
    @Log(title = "重新统计", businessType = BusinessType.UPDATE)
    @PostMapping("/calc/{priceMonth}")
    public AjaxResult calc(@PathVariable String priceMonth) {
        String date = priceMonth + "-01";
        LocalDate start = LocalDate.parse(date);
        LocalDate now = LocalDate.now();
        LocalDate end = LocalDateTime.now().minusHours(7).minusDays(1).toLocalDate();
        if (!YearMonth.from(start).equals(YearMonth.from(now))) {
            end = start.withDayOfMonth(start.lengthOfMonth());
        }
        while (!start.isAfter(end)) {
            System.err.println(start);
            workshopCalcService.calc(start);
            start = start.plusDays(1);
        }
        return AjaxResult.success();
    }

    /**
     * 删除电费价格配置
     */
    @ApiOperation("删除电费价格配置")
    //@PreAuthorize("@ss.hasPermi('elec:price:remove')")
    @Log(title = "电费价格配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{priceIds}")
    public AjaxResult remove(@PathVariable Long[] priceIds) {
        return toAjax(elecPriceService.deleteElecPriceByPriceIds(priceIds));
    }
}
