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

import com.cpop.core.base.entity.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.StoreListVo;
import com.cpop.oam.business.vo.TaskDemandPageVo;
import com.cpop.oam.business.vo.TaskDemandUrgentVo;
import com.cpop.oam.business.vo.BrandListVo;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.row.DbChain;
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.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

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

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

    @Autowired
    private TaskDemandService taskDemandService;

    /**
     * 工单模块-需求-需求分页列表
     * @author DB
     * @param bo 查询参数
     * @since 2023-11-30 17:26:24
     */
    @Operation(summary = "工单模块-需求-需求分页列表")
    @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
     */
    @Operation(summary = "工单模块-需求-新增需求")
    @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
     */
    @Operation(summary = "工单模块-需求-修改需求")
    @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
     */
    @Operation(summary = "工单模块-需求-删除需求")
    @DeleteMapping("/removeDemandTask/{demandId}")
    public R<Void> removeDemandTask(@PathVariable @Parameter(description = "需求id") String demandId) {
        taskDemandService.removeDemandTask(demandId);
        return R.ok();
    }

    /**
     * 任务管理模块-工单管理-获取品牌列表
     * @author DB
     * @param brandName 品牌名
     * @since 2023-11-30 17:59:29
     */
    @Operation(summary = "工单模块-工单提交-获取品牌列表")
    @GetMapping("/getBrandList")
    public R<List<BrandListVo>> getBrandList(@Parameter(description = "品牌名") String brandName) {
        AtomicReference<List<BrandListVo>> list = new AtomicReference<>();
        try {
            DataSourceKey.use("jambox", () -> {
                list.set(DbChain.table(BRAND).and(BRAND.BRAND_NAME.like(brandName)).listAs(BrandListVo.class));
            });
        } finally {
            DataSourceKey.clear();
        }
        return R.ok(list.get());
    }

    /**
     * 工单模块-工单提交-获取校区/店铺列表
     * @author DB
     * @param brandId 品牌id
     * @since 2023-11-30 18:00:08
     */
    @Operation(summary = "工单模块-工单提交-获取校区/店铺列表")
    @GetMapping("/getStoreList")
    public R<List<StoreListVo>> getStoreList(@Parameter(description = "品牌id") String brandId) {
        AtomicReference<List<StoreListVo>> list = new AtomicReference<>();
        try {
            DataSourceKey.use("jambox", () -> {
                list.set(DbChain.table(STORE).and(STORE.BRAND_ID.like(brandId)).listAs(StoreListVo.class));
            });
        } finally {
            DataSourceKey.clear();
        }
        return R.ok(list.get());
    }

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

}
