package com.yc.ship.module.otc.controller.admin.storescheduling;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.otc.controller.admin.storescheduling.vo.StoreSchedulingPageReqVO;
import com.yc.ship.module.otc.controller.admin.storescheduling.vo.StoreSchedulingRespVO;
import com.yc.ship.module.otc.controller.admin.storescheduling.vo.StoreSchedulingSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storescheduling.StoreSchedulingDO;
import com.yc.ship.module.otc.service.storescheduling.StoreSchedulingService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 班次管理")
@RestController
@RequestMapping("/store-scheduling")
@Validated
public class StoreSchedulingController {

    @Resource
    private StoreSchedulingService storeSchedulingService;

    @PostMapping("/create")
    @Operation(summary = "创建班次管理")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling:create')")
    public CommonResult<Long> createStoreScheduling(@Valid @RequestBody StoreSchedulingSaveReqVO createReqVO) {
        return success(storeSchedulingService.createStoreScheduling(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班次管理")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling:update')")
    public CommonResult<Boolean> updateStoreScheduling(@Valid @RequestBody StoreSchedulingSaveReqVO updateReqVO) {
        storeSchedulingService.updateStoreScheduling(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班次管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling:delete')")
    public CommonResult<Boolean> deleteStoreScheduling(@RequestParam("id") Long id) {
        storeSchedulingService.deleteStoreScheduling(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班次管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling:query')")
    public CommonResult<StoreSchedulingRespVO> getStoreScheduling(@RequestParam("id") Long id) {
        StoreSchedulingDO storeScheduling = storeSchedulingService.getStoreScheduling(id);
        return success(BeanUtils.toBean(storeScheduling, StoreSchedulingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班次管理分页")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling:query')")
    public CommonResult<PageResult<StoreSchedulingRespVO>> getStoreSchedulingPage(@Valid StoreSchedulingPageReqVO pageReqVO) {
        PageResult<StoreSchedulingDO> pageResult = storeSchedulingService.getStoreSchedulingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreSchedulingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班次管理 Excel")
    @PreAuthorize("@ss.hasPermission('otc:store-scheduling:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportStoreSchedulingExcel(@Valid StoreSchedulingPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreSchedulingDO> list = storeSchedulingService.getStoreSchedulingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班次管理.xls", "数据", StoreSchedulingRespVO.class,
                BeanUtils.toBean(list, StoreSchedulingRespVO.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取站点精简信息列表", description = "只包含被启用的站点，主要用于前端的下拉选项")
    public CommonResult<List<StoreSchedulingRespVO>> getSimpleSchedulingList() {
        List<StoreSchedulingDO> list = storeSchedulingService.getSchedulingListByStatus();
        return success(BeanUtils.toBean(list, StoreSchedulingRespVO.class));
    }

}
