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

import com.cpop.core.base.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.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.core.base.table.table.SysUserTableDef.SYS_USER;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;

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

    @Autowired
    private DutyService dutyService;

    @Autowired
    private StaffService staffService;

    /**
     * @Description: 查询值班列表(当月)
     * @param bo 请求参数
     * @return: R<List<DutyListVo>>
     * @Author: DB
     * @Date: 2023/5/11 16:39
     **/
    @PreAuthorize("@aps.hasPermission('dutyCalendar:duty:list')")
    @ApiOperation("查询值班列表(当月)")
    @GetMapping("/getDutyList")
    public R<List<DutyListVo>> getDutyList(DutyListBo bo) {
        List<DutyListVo> list = dutyService.getDutyList(bo);
        return R.ok(list);
    }

    /**
     * @Description: 新增值班
     * @param bo 请求参数
     * @return: R<Void>
     * @Author: DB
     * @Date: 2023/5/18 9:50
     **/
    @PreAuthorize("@aps.hasPermission('dutyCalendar:duty:management')")
    @ApiOperation("新增值班")
    @PostMapping("/insertDuty")
    public R<Void> insertDuty(@RequestBody @Validated DutyBo bo) {
        dutyService.insertDuty(bo);
        return R.ok();
    }

    /**
     * @descriptions 删除值班信息
     * @author DB
     * @date 2023/09/14 18:16
     * @param dutyDate 值班日期
     * @return com.pupu.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('dutyCalendar:duty:management')")
    @ApiOperation("删除值班信息")
    @DeleteMapping("/removeDutyByDate/{dutyDate}")
    public R<Void> removeDutyByDate(@PathVariable String dutyDate) {
        dutyService.removeDutyByDate(dutyDate);
        return R.ok();
    }

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

    /**
     * @Description: 查询技术员工列表
     * @return: R<PageVo<StaffPageVo>>
     * @Author: DB
     * @Date: 2023/5/11 17:50
     **/
    @ApiOperation("查询技术员工列表")
    @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(SYS_USER).on(SYS_USER.ID.eq(STAFF.USER_ID))
                        .where(STAFF.STAFF_TYPE.eq(0))
                        //不查询停用的员工
                        .and(SYS_USER.STATUS.eq(true)),
                StaffVo.class);
        return R.ok(list);
    }

}
