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

import cn.iocoder.yudao.module.system.controller.admin.cashregisterinfo.vo.CashRegisterInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.cashregisterinfo.vo.CashRegisterInfoRespVO;
import cn.iocoder.yudao.module.system.controller.admin.cashregisterinfo.vo.CashRegisterInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.cashregisterinfo.CashRegisterInfoDO;
import cn.iocoder.yudao.module.system.service.cashregisterinfo.CashRegisterInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 收银机信息关联门店")
@RestController
@RequestMapping("/t/cash-register-info")
@Validated
public class CashRegisterInfoController {

    @Resource
    private CashRegisterInfoService cashRegisterInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建收银机信息关联门店")
    @PreAuthorize("@ss.hasPermission('t:cash-register-info:create')")
    public CommonResult<Long> createCashRegisterInfo(@Valid @RequestBody CashRegisterInfoSaveReqVO createReqVO) {
        return success(cashRegisterInfoService.createCashRegisterInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收银机信息关联门店")
    @PreAuthorize("@ss.hasPermission('t:cash-register-info:update')")
    public CommonResult<Boolean> updateCashRegisterInfo(@Valid @RequestBody CashRegisterInfoSaveReqVO updateReqVO) {
        cashRegisterInfoService.updateCashRegisterInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收银机信息关联门店")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:cash-register-info:delete')")
    public CommonResult<Boolean> deleteCashRegisterInfo(@RequestParam("id") Long id) {
        cashRegisterInfoService.deleteCashRegisterInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收银机信息关联门店")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:cash-register-info:query')")
    public CommonResult<CashRegisterInfoRespVO> getCashRegisterInfo(@RequestParam("id") Long id) {
        CashRegisterInfoDO cashRegisterInfo = cashRegisterInfoService.getCashRegisterInfo(id);
        return success(BeanUtils.toBean(cashRegisterInfo, CashRegisterInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收银机信息关联门店分页")
    @PreAuthorize("@ss.hasPermission('t:cash-register-info:query')")
    public CommonResult<PageResult<CashRegisterInfoRespVO>> getCashRegisterInfoPage(@Valid CashRegisterInfoPageReqVO pageReqVO) {
        PageResult<CashRegisterInfoDO> pageResult = cashRegisterInfoService.getCashRegisterInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CashRegisterInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收银机信息关联门店 Excel")
    @PreAuthorize("@ss.hasPermission('t:cash-register-info:export')")
    @OperateLog(type = EXPORT)
    public void exportCashRegisterInfoExcel(@Valid CashRegisterInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CashRegisterInfoDO> list = cashRegisterInfoService.getCashRegisterInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收银机信息关联门店.xls", "数据", CashRegisterInfoRespVO.class,
                        BeanUtils.toBean(list, CashRegisterInfoRespVO.class));
    }

}