package cn.iocoder.yudao.module.system.controller.admin.devicemoney;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.dal.dataobject.devicemoney.DeviceMoneyDO;
import cn.iocoder.yudao.module.system.service.devicemoney.DeviceMoneyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.iocoder.yudao.module.system.controller.admin.devicemoney.vo.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 门店设备日流水")
@RestController
@RequestMapping("/t/device-money")
@Validated
public class DeviceMoneyController {

    @Resource
    private DeviceMoneyService deviceMoneyService;

    @PostMapping("/create")
    @Operation(summary = "创建门店设备日流水")
    @PreAuthorize("@ss.hasPermission('t:device-money:create')")
    public CommonResult<Long> createDeviceMoney(@Valid @RequestBody DeviceMoneySaveReqVO createReqVO) {
        return success(deviceMoneyService.createDeviceMoney(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门店设备日流水")
    @PreAuthorize("@ss.hasPermission('t:device-money:update')")
    public CommonResult<Boolean> updateDeviceMoney(@Valid @RequestBody DeviceMoneySaveReqVO updateReqVO) {
        deviceMoneyService.updateDeviceMoney(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店设备日流水")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:device-money:delete')")
    public CommonResult<Boolean> deleteDeviceMoney(@RequestParam("id") Long id) {
        deviceMoneyService.deleteDeviceMoney(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门店设备日流水")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:device-money:query')")
    public CommonResult<DeviceMoneyRespVO> getDeviceMoney(@RequestParam("id") Long id) {
        DeviceMoneyDO deviceMoney = deviceMoneyService.getDeviceMoney(id);
        return success(BeanUtils.toBean(deviceMoney, DeviceMoneyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门店设备日流水分页")
    @PreAuthorize("@ss.hasPermission('t:device-money:query')")
    public CommonResult<PageResult<DeviceMoneyRespVO>> getDeviceMoneyPage(@Valid DeviceMoneyPageReqVO pageReqVO) {
        PageResult<DeviceMoneyDO> pageResult = deviceMoneyService.getDeviceMoneyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceMoneyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店设备日流水 Excel")
    @PreAuthorize("@ss.hasPermission('t:device-money:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceMoneyExcel(@Valid DeviceMoneyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceMoneyDO> list = deviceMoneyService.getDeviceMoneyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门店设备日流水.xls", "数据", DeviceMoneyRespVO.class,
                        BeanUtils.toBean(list, DeviceMoneyRespVO.class));
    }

}