package com.ruoyi.web.bst;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.changeRecord.domain.ChangeRecordQuery;
import com.ruoyi.bst.changeRecord.domain.ChangeRecordVO;
import com.ruoyi.bst.changeRecord.domain.enums.ChangeRecordBstType;
import com.ruoyi.bst.changeRecord.service.ChangeRecordService;
import com.ruoyi.bst.store.service.StoreValidator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.user.service.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 爆灯次数变化Controller
 *
 * @author ruoyi
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/bst/changeRecord")
public class ChangeRecordController extends BaseController
{
    @Autowired
    private ChangeRecordService changeRecordService;
    @Autowired
    private UserValidator userValidator;
    @Autowired
    private StoreValidator storeValidator;

    /**
     * 查询爆灯次数变化列表
     */
    @PreAuthorize("@ss.hasPermi('bst:changeRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChangeRecordQuery query)
    {
        startPage();
        startOrderBy();
        query.setScope(true);
        List<ChangeRecordVO> list = changeRecordService.selectChangeRecordList(query);
        return getDataTable(list);
    }

    /**
     * 导出爆灯次数变化列表
     */
    @PreAuthorize("@ss.hasPermi('bst:changeRecord:export')")
    @Log(title = "爆灯次数变化", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChangeRecordQuery query)
    {
        query.setScope(true);
        List<ChangeRecordVO> list = changeRecordService.selectChangeRecordList(query);
        ExcelUtil<ChangeRecordVO> util = new ExcelUtil<ChangeRecordVO>(ChangeRecordVO.class);
        util.exportExcel(response, list, "爆灯次数变化数据");
    }

    /**
     * 获取爆灯次数变化详细信息
     */
    @PreAuthorize("@ss.hasPermi('bst:changeRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(changeRecordService.selectChangeRecordByRecordId(recordId,true));
    }


    /**
     * 删除爆灯次数变化
     */
    @PreAuthorize("@ss.hasPermi('bst:changeRecord:remove')")
    @Log(title = "爆灯次数变化", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(changeRecordService.deleteChangeRecordByRecordIds(recordIds));
    }


    @PreAuthorize("@ss.hasPermi('bst:changeRecord:consumeRecord')")
    @Log(title = "爆灯次数变化")
    @GetMapping("/consumeRecord")
    public AjaxResult consumeRecord(ChangeRecordQuery query) {
        if (query.getUserId() == null) {
            return error("用户ID不可以为空");
        }
        if (!userValidator.canView(query.getUserId())){
            return error("您无权限查看该用户信息");
        }
        if (!storeValidator.canEdit(query.getStoreId())){
            return error("您无权限查看该店铺信息");
        }
        // 查询爆灯消费列表
        query.setScope(true);
        query.setBstTypeList(ChangeRecordBstType.numIncrease());
        List<ChangeRecordVO> lightingList = changeRecordService.selectChangeRecordList(query);
        // 查询充值列表
        query.setBstTypeList(ChangeRecordBstType.used());
        List<ChangeRecordVO> rechrageList = changeRecordService.selectChangeRecordList(query);
        // 合并列表数据
        ArrayList<ChangeRecordVO> list = new ArrayList<ChangeRecordVO>();
        list.addAll(lightingList);
        list.addAll(rechrageList);
        return AjaxResult.success(list);
    }
}
