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

import com.cpop.core.base.R;
import com.cpop.jambox.business.vo.BrandListVo;
import com.cpop.oam.business.bo.AgreeOrRejectEnforceFinishBo;
import com.cpop.oam.business.bo.PauseWorkOrderBo;
import com.cpop.oam.business.bo.TaskWorkOrderBo;
import com.cpop.oam.business.bo.TaskWorkOrderRecordBo;
import com.cpop.oam.business.service.TaskWorkOrderService;
import com.cpop.oam.business.vo.*;
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.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

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

/**
 * 工单模块
 * @author DB
 * @since 2023-09-18
 */
@RestController
@Api(tags = "工单模块-工单")
@RequestMapping("/backstage/taskWorkOrder")
public class BackstageTaskWorkOrderController {

    @Autowired
    private TaskWorkOrderService taskWorkOrderService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private StoreService storeService;

    /**
     * 工单模块-工单提交-接收/处理/暂停中
     * @author DB
     * @since 2023/09/18 17:18
     * @return com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.oam.business.vo.TaskWorkOrderPageVo>>
     */
    @PreAuthorize("@aps.hasPermission('oamTask:workOrder:list')")
    @ApiOperation("工单模块-工单提交-接收/处理/暂停中")
    @GetMapping("/receiveDealPause")
    public R<TaskWorkOrderReceiveDealPauseVo> receiveDealPause() {
        TaskWorkOrderReceiveDealPauseVo vo = taskWorkOrderService.receiveDealPause();
        return R.ok(vo);
    }

    /**
     * 工单模块-工单提交-获取当天值班人员
     * @author DB
     * @since 2023-11-29 14:01:03
     */
    @ApiOperation("工单模块-工单提交-获取当天值班人员")
    @GetMapping("/getWorkOrderDutyStaff")
    public R<WorkOrderDutyVo> getWorkOrderDutyStaff() {
        WorkOrderDutyVo vo = taskWorkOrderService.getWorkOrderDutyStaff();
        return R.ok(vo);
    }

    /**
     * 工单模块-工单提交-已办结分页列表
     * @author DB
     * @since 2023-11-29 14:35:08
     */
    @ApiOperation("工单模块-工单提交-已办结分页列表")
    @GetMapping("/finishWorkOrderPage")
    public R<Page<TaskWorkOrderPageVo>> finishWorkOrderPage() {
        Page<TaskWorkOrderPageVo> page = taskWorkOrderService.finishWorkOrderPage();
        return R.ok(page);
    }

    /**
     * 工单模块-工单提交-新增工单
     * @param bo 请求参数         
     * @return R<Void>
     * @author DB
     * @since 2023/5/30 16:53
     **/
    @ApiOperation("工单模块-工单提交-新增工单")
    @PostMapping("/insertWorkOrder")
    public R<Void> insertWorkOrder(@RequestBody @Validated TaskWorkOrderBo bo) {
        taskWorkOrderService.insertWorkOrder(bo);
        return R.ok();
    }

    /**
     * 工单模块-工单提交-工单记录列表
     * @author DB
     * @param workOrderId 工单id
     * @since 2023-11-29 11:25:28
     */
    @ApiOperation("工单模块-工单提交-工单记录列表")
    @GetMapping("/getWorkOrderRecordList/{workOrderId}")
    public R<List<TaskWorkOrderRecordListVo>> getWorkOrderRecordList(@PathVariable @ApiParam("工单id") String workOrderId) {
        List<TaskWorkOrderRecordListVo> list = taskWorkOrderService.getWorkOrderRecordList(workOrderId);
        return R.ok(list);
    }

    /**
     * 工单模块-工单提交-新增工单记录
     * @author DB
     * @param bo 请求参数
     * @since 2023-11-29 11:33:35
     */
    @ApiOperation("工单模块-工单提交-新增工单记录")
    @PostMapping("/insertWorkOrderRecord")
    public R<Void> insertWorkOrderRecord(@RequestBody @Validated TaskWorkOrderRecordBo bo) {
        taskWorkOrderService.insertWorkOrderRecord(bo);
        return R.ok();
    }

    /**
     * 工单模块-工单提交-工单提醒
     * @author DB
     * @param workOrderId 工单id
     * @since 2023-11-29 11:41:46
     */
    @ApiOperation("工单模块-工单提交-工单提醒")
    @PutMapping("/workOrderRemind/{workOrderId}")
    public R<Void> workOrderRemind(@PathVariable @ApiParam("工单id") String workOrderId) {
        taskWorkOrderService.workOrderRemind(workOrderId);
        return R.ok();
    }

    /**
     * 工单模块-工单提交-工单转需求
     * @author DB
     * @param workOrderId 工单id
     * @since 2023-11-29 12:30:21
     */
    @ApiOperation("工单模块-工单提交-工单转需求")
    @PutMapping("/workOrderToDemand/{workOrderId}")
    public R<Void> workOrderToDemand(@PathVariable @ApiParam("工单id") String workOrderId) {
        taskWorkOrderService.workOrderToDemand(workOrderId);
        return R.ok();
    }

    /**
     * 工单模块-工单提交-工单暂停
     * @author DB
     * @param bo 请求参数
     * @since 2023-11-29 16:00:18
     */
    @ApiOperation("工单模块-工单提交-工单暂停")
    @PutMapping("/pauseWorkOrder")
    public R<Void> pauseWorkOrder(@RequestBody @Validated PauseWorkOrderBo bo) {
        taskWorkOrderService.pauseWorkOrder(bo);
        return R.ok();
    }

    /**
     * 工单模块-工单提交-工单办结
     * @author DB
     * @param workOrderId 工单id
     * @since 2023-11-29 15:44:08
     */
    @ApiOperation("工单模块-工单提交-工单办结")
    @PutMapping("/workOrderFinish/{workOrderId}")
    public R<Void> workOrderFinish(@PathVariable("workOrderId") @ApiParam("工单id") String workOrderId) {
        taskWorkOrderService.workOrderFinish(workOrderId);
        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 2024/1/31
     * @param staffId 员工id
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return R<Page<TaskWorkOrderPageVo>>
     */
    @ApiOperation("工单统计-获取工单统计分页")
    @GetMapping("/getWorkOrderStatPage")
    public R<Page<TaskWorkOrderStatPageVo>> getWorkOrderStatPage(@ApiParam("员工id") @RequestParam(value = "staffId", required = false) String staffId,
                                                                 @RequestParam(value = "startDate", required = false) @ApiParam("开始日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
                                                                 @RequestParam(value = "endDate", required = false) @ApiParam("结束日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        Page<TaskWorkOrderStatPageVo> page = taskWorkOrderService.getWorkOrderStatPage(staffId, startDate, endDate);
        return R.ok(page);
    }

    /**
     * 同意与拒绝强制办结
     * @author DB
     * @since 2024/2/21
     * @param bo 请求参数
     * @return R<Void>
     */
    @ApiOperation("工单模块-工单提交-同意与拒绝强制办结")
    @PutMapping("/agreeOrRejectEnforceFinish")
    public R<Void> agreeOrRejectEnforceFinish(@RequestBody @Validated AgreeOrRejectEnforceFinishBo bo) {
        taskWorkOrderService.agreeOrRejectEnforceFinish(bo);
        return R.ok();
    }
}
