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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.carteen.vo.CarteenPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.carteen.vo.CarteenRespVO;
import cn.iocoder.yudao.module.system.controller.admin.carteen.vo.CarteenSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.carteen.CarteenDO;
import cn.iocoder.yudao.module.system.service.carteen.CarteenService;
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.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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/carteen")
@Validated
public class CarteenController {

    @Resource
    private CarteenService carteenService;

    @PostMapping("/create")
    @Operation(summary = "创建门店管理")
    @PreAuthorize("@ss.hasPermission('t:carteen:create')")
    public CommonResult<Long> createCarteen(@Valid @RequestBody CarteenSaveReqVO createReqVO) {
        return success(carteenService.createCarteen(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门店管理")
    @PreAuthorize("@ss.hasPermission('t:carteen:update')")
    public CommonResult<Boolean> updateCarteen(@Valid @RequestBody CarteenSaveReqVO updateReqVO) {
        carteenService.updateCarteen(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:carteen:delete')")
    public CommonResult<Boolean> deleteCarteen(@RequestParam("id") Long id) {
        carteenService.deleteCarteen(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门店管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:carteen:query')")
    public CommonResult<CarteenRespVO> getCarteen(@RequestParam("id") Long id) {
        CarteenDO carteen = carteenService.getCarteen(id);
        return success(BeanUtils.toBean(carteen, CarteenRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门店管理分页")
    @PreAuthorize("@ss.hasPermission('t:carteen:query')")
    public CommonResult<PageResult<CarteenRespVO>> getCarteenPage(@Valid CarteenPageReqVO pageReqVO) {
        PageResult<CarteenDO> pageResult = carteenService.getCarteenPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CarteenRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店管理 Excel")
    @PreAuthorize("@ss.hasPermission('t:carteen:export')")
    @OperateLog(type = EXPORT)
    public void exportCarteenExcel(@Valid CarteenPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarteenDO> list = carteenService.getCarteenPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门店管理.xls", "数据", CarteenRespVO.class,
                        BeanUtils.toBean(list, CarteenRespVO.class));
    }

}