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

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.controller.admin.carteen.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.carteen.CarteenDO;
import cn.iocoder.yudao.module.system.service.carteen.CarteenService;
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 javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

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/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 = "获得门店管理分页")
    @PermitAll
    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));
    }
    @GetMapping("/week/sales")
    @Operation(summary = "获得门店销量")
    @PreAuthorize("@ss.hasPermission('t:carteen:query')")
    public CommonResult<Map<String, List<CarteenWeekSalesRespVo>>> getCarteenSales(CarteenPageReqVO pageReqVO) {
        return success(carteenService.getCarteenWeekSales(pageReqVO));
    }
    @GetMapping("/user/carteem")
    @Operation(summary = "获取当前用户有那些门店")
    @PreAuthorize("@ss.hasPermission('t:carteen:query')")
    public CommonResult<List<CarteenRespVO>> getUserCarteem(@RequestParam("userId")Long userId) {
        return success(BeanUtils.toBean(carteenService.getUserCarteem(userId), CarteenRespVO.class));
    }
    @PutMapping("/user/update/carteen")
    @Operation(summary = "更新门店关联用户")
    @PreAuthorize("@ss.hasPermission('t:carteen:update')")
    public CommonResult<Boolean> updateUserCarteen(@Valid @RequestBody CarteenSerialSaveReqVO req) {
        return success(carteenService.updateUserCarteen(req));
    }

    @GetMapping("/list")
    @Operation(summary = "获得门店管理列表")
    @PermitAll
    public CommonResult<List<CarteenRespVO>> getCarteenList(@Valid CarteenPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CarteenDO> pageResult = carteenService.getCarteenPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, CarteenRespVO.class));
    }

}