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

import com.cpop.core.base.entity.R;
import com.cpop.oam.business.bo.DutyBo;
import com.cpop.oam.business.bo.DutyListBo;
import com.cpop.oam.business.service.DutyService;
import com.cpop.oam.business.service.StaffService;
import com.cpop.oam.business.vo.DutyListVo;
import com.cpop.oam.business.vo.StaffVo;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
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 static com.cpop.core.base.table.table.UserTableDef.USER;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;

/**
 * 值班表 控制层。
 *
 * @author DB
 * @since 2023-09-14
 */
@RestController
@Tag(name = "值班日历模块")
@RequestMapping("/backstage/duty")
public class BackstageDutyController {

    @Autowired
    private DutyService dutyService;

    @Autowired
    private StaffService staffService;

    /**
     * 查询值班列表(当月)
     * @param bo 请求参数
     * @return R<List<DutyListVo>>
     * @author DB
     * @since 2023/5/11 16:39
     **/
    @Operation(summary = "查询值班列表(当月)")
    @GetMapping("/getDutyList")
    public R<List<DutyListVo>> getDutyList(DutyListBo bo) {
        List<DutyListVo> list = dutyService.getDutyList(bo);
        return R.ok(list);
    }

    /**
     * 新增值班
     * @param bo 请求参数
     * @return R<Void>
     * @author DB
     * @since 2023/5/18 9:50
     **/
    @Operation(summary = "新增值班")
    @PostMapping("/insertDuty")
    public R<Void> insertDuty(@RequestBody @Validated DutyBo bo) {
        dutyService.insertDuty(bo);
        return R.ok();
    }

    /**
     * 删除值班信息
     * @author DB
     * @since 2023/09/14 18:16
     * @param dutyDate 值班日期
     * @return com.pupu.core.base.R<java.lang.Void>
     */
    @Operation(summary = "删除值班信息")
    @DeleteMapping("/removeDutyByDate/{dutyDate}")
    public R<Void> removeDutyByDate(@PathVariable String dutyDate) {
        dutyService.removeDutyByDate(dutyDate);
        return R.ok();
    }

    /**
     * 查询服务员工列表
     * @return R<List<OamStaffVo>>
     * @author DB
     * @since 2023/5/11 17:49
     **/
    @Operation(summary = "查询服务员工列表")
    @GetMapping("/getServiceStaffList")
    public R<List<StaffVo>> getServiceStaffList() {
        List<StaffVo> list = staffService.getServiceStaffList();
        return R.ok(list);
    }

    /**
     * 查询技术员工列表
     * @return R<PageVo<StaffPageVo>>
     * @author DB
     * @since 2023/5/11 17:50
     **/
    @Operation(summary = "查询技术员工列表")
    @GetMapping("/getTechnologyStaffList")
    public R<List<StaffVo>> getTechnologyStaffList() {
        List<StaffVo> list = staffService.listAs(QueryWrapper.create()
                        .select(STAFF.ID, STAFF.NAME, STAFF.USER_ID, STAFF.STAFF_TYPE)
                        .from(STAFF)
                        .leftJoin(USER).on(USER.ID.eq(STAFF.USER_ID))
                        .where(STAFF.STAFF_TYPE.eq(0))
                        //不查询停用的员工
                        .and(USER.STATUS.eq(true)),
                StaffVo.class);
        return R.ok(list);
    }

}
