package com.cpop.oam.business.controller.backstage;

import com.cpop.core.base.R;
import com.cpop.oam.business.bo.TaskDemandBo;
import com.cpop.oam.business.bo.TaskDemandPageBo;
import com.cpop.oam.business.bo.TaskDemandUrgentBo;
import com.cpop.oam.business.service.TaskDemandService;
import com.cpop.oam.business.vo.TaskDemandUrgentVo;
import com.cpop.system.business.vo.BrandListVo;
import com.cpop.oam.business.vo.StoreListVo;
import com.cpop.oam.business.vo.TaskDemandPageVo;
import com.cpop.system.business.service.BrandService;
import com.cpop.system.business.service.StoreService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;

/**
 * OAM-任务-需求表 控制层。
 *
 * @author DB
 * @since 2023-09-18
 */
@RestController
@Api(tags = "工单模块-需求")
@RequestMapping("/backstage/taskDemand")
public class BackstageTaskDemandController {

    @Autowired
    private TaskDemandService taskDemandService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private StoreService storeService;

    /**
     * 工单模块-需求-需求分页列表
     * @author DB
     * @param bo 查询参数
     * @since 2023-11-30 17:26:24
     */
    @PreAuthorize("@aps.hasPermission('oamTask:taskDemand:list')")
    @ApiOperation("工单模块-需求-需求分页列表")
    @GetMapping("/getTaskDemandPage")
    public R<Page<TaskDemandPageVo>> getTaskDemandPage(TaskDemandPageBo bo) {
        Page<TaskDemandPageVo> pageVo = taskDemandService.getTaskDemandPage(bo);
        return R.ok(pageVo);
    }

    /**
     * 工单模块-需求-新增需求
     * @author DB
     * @param bo 请求参数
     * @since 2023-11-30 17:27:24
     */
    @ApiOperation("工单模块-需求-新增需求")
    @PostMapping("/insertDemandTask")
    public R<Void> insertDemandTask(@RequestBody @Validated
    TaskDemandBo bo) {
        taskDemandService.insertTaskDemand(bo);
        return R.ok();
    }

    /**
     * 工单模块-需求-修改需求
     * @author DB
     * @param bo 请求参数
     * @since 2023-11-30 18:26:42
     */
    @ApiOperation("工单模块-需求-修改需求")
    @PutMapping("/updateDemandTask")
    public R<Void> updateDemandTask(@RequestBody @Validated
    TaskDemandBo bo) {
        taskDemandService.updateDemandTask(bo);
        return R.ok();
    }

    /**
     * 工单模块-需求-删除需求
     * @author DB
     * @param demandId 需求id
     * @since 2023-11-30 20:30:54
     */
    @ApiOperation("工单模块-需求-删除需求")
    @DeleteMapping("/removeDemandTask/{demandId}")
    public R<Void> removeDemandTask(@PathVariable @ApiParam(value = "需求id")
    String demandId) {
        taskDemandService.removeDemandTask(demandId);
        return R.ok();
    }

    /**
     * 任务管理模块-工单管理-获取品牌列表
     * @author DB
     * @param brandName 品牌名
     * @since 2023-11-30 17:59:29
     */
    @ApiOperation("工单模块-工单提交-获取品牌列表")
    @GetMapping("/getBrandList")
    public R<List<BrandListVo>> getBrandList(@ApiParam("品牌名")
    String brandName) {
        List<BrandListVo> list =
            brandService.queryChain().and(BRAND.BRAND_NAME.like(brandName)).listAs(BrandListVo.class);
        return R.ok(list);
    }

    /**
     * 工单模块-工单提交-获取校区/店铺列表
     * @author DB
     * @param brandId 品牌id
     * @since 2023-11-30 18:00:08
     */
    @ApiOperation("工单模块-工单提交-获取校区/店铺列表")
    @GetMapping("/getStoreList")
    public R<List<StoreListVo>> getStoreList(@ApiParam("品牌id")
    String brandId) {
        List<StoreListVo> list = storeService.queryChain().and(STORE.BRAND_ID.eq(brandId)).listAs(StoreListVo.class);
        return R.ok(list);
    }

    /**
     * 工单模块-需求-需求加急
     * @author DB
     * @since 2023/12/08
     * @param bo 请求参数
     * @return R<Void>
     */
    @ApiOperation("工单模块-需求-需求加急")
    @PutMapping("/demandUrgent")
    public R<TaskDemandUrgentVo> demandUrgent(@RequestBody @Validated TaskDemandUrgentBo bo) {
        TaskDemandUrgentVo vo = taskDemandService.demandUrgent(bo);
        return R.ok(vo);
    }

}
