package com.lideeyunji.service.system.controller;

import com.lideeyunji.tool.framework.common.enums.CommonStatusEnum;
import com.lideeyunji.tool.framework.common.pojo.CommonResult;
import com.lideeyunji.tool.framework.common.pojo.PageParam;
import com.lideeyunji.tool.framework.common.pojo.PageResult;
import com.lideeyunji.tool.framework.common.util.object.BeanUtils;
import com.lideeyunji.tool.framework.excel.core.util.ExcelUtils;
import com.lideeyunji.tool.framework.operatelog.core.annotations.OperateLog;
import com.lideeyunji.service.system.controller.vo.dept.duty.DutyPageReqVO;
import com.lideeyunji.service.system.controller.vo.dept.duty.DutyRespVO;
import com.lideeyunji.service.system.controller.vo.dept.duty.DutySaveReqVO;
import com.lideeyunji.service.system.controller.vo.dept.duty.DutySimpleRespVO;
import com.lideeyunji.service.system.entity.DutyDO;
import com.lideeyunji.service.system.service.IDutyService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.lideeyunji.tool.framework.common.pojo.CommonResult.success;
import static com.lideeyunji.tool.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 职务")
@RestController
@RequestMapping("/system/duty")
@Validated
public class DutyController {

    @Resource
    private IDutyService dutyService;

    @PostMapping("/create")
    @Operation(tags = "职务管理",summary = "创建职务")
    @PreAuthorize("@ss.hasPermission('system:duty:create')")
    public CommonResult<Long> create(@Valid @RequestBody DutySaveReqVO createReqVO) {
        Long id = dutyService.createDuty(createReqVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(tags = "职务管理",summary = "修改职务")
    @PreAuthorize("@ss.hasPermission('system:duty:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody DutySaveReqVO updateReqVO) {
        dutyService.updateDuty(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(tags = "职务管理",summary = "删除职务")
    @PreAuthorize("@ss.hasPermission('system:duty:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        dutyService.deleteDuty(id);
        return success(true);
    }

    @GetMapping(value = "/get")
    @Operation(tags = "职务管理",summary = "获得职务信息")
    @Parameter(name = "id", description = "职务编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:duty:query')")
    public CommonResult<DutyRespVO> get(@RequestParam("id") Long id) {
        DutyDO duty = dutyService.getDuty(id);
        return success(BeanUtils.toBean(duty, DutyRespVO.class));
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @Operation(tags = "职务管理",summary = "获取职务全列表", description = "只包含被开启的职务，主要用于前端的下拉选项")
    public CommonResult<List<DutySimpleRespVO>> getSimpleList() {
        // 获得职务列表，只要开启状态的
        List<DutyDO> list = dutyService.getDutyList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(DutyDO::getSort));
        return success(BeanUtils.toBean(list, DutySimpleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(tags = "职务管理",summary = "获得职务分页列表")
    @PreAuthorize("@ss.hasPermission('system:duty:query')")
    public CommonResult<PageResult<DutyRespVO>> getPage(@Validated DutyPageReqVO pageReqVO) {
        PageResult<DutyDO> pageResult = dutyService.getDutyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DutyRespVO.class));
    }

    @GetMapping("/export")
    @Operation(tags = "职务管理",summary = "职务管理")
    @PreAuthorize("@ss.hasPermission('system:duty:export')")
    @OperateLog(type = EXPORT)
    public void export(HttpServletResponse response, @Validated DutyPageReqVO reqVO) throws IOException {
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DutyDO> list = dutyService.getDutyPage(reqVO).getList();
        // 输出
        ExcelUtils.write(response, "职务数据.xls", "职务列表", DutyRespVO.class,
                BeanUtils.toBean(list, DutyRespVO.class));
    }

}
