package com.jeeplus.test.rank.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.test.rank.domain.RankLevel;
import com.jeeplus.test.rank.domain.RankSequence;
import com.jeeplus.test.rank.service.RankLevelService;
import com.jeeplus.test.rank.service.RankSequenceService;
import com.jeeplus.test.rank.service.dto.RankLevelDTO;
import com.jeeplus.test.rank.service.dto.RankSequenceDTO;
import com.jeeplus.test.rank.service.mapstruct.RankLevelWrapper;
import com.jeeplus.test.rank.service.mapstruct.RankSequenceWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;

@Api("职级体系-职层管理")
@RestController
@RequestMapping(value = "/rank/rank_level")
public class RankLevelController {

    @Autowired
    private RankLevelService rankLevelService;

    /**
     * 查询职层列表
     * @param rankLevelDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询职层列表")
    @PreAuthorize("hasAuthority('rank:rank_level:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<RankLevel>> data(RankLevelDTO rankLevelDTO, Page<RankLevel> page) throws Exception {
        RankLevel rankLevel = RankLevelWrapper.INSTANCE.toEntity(rankLevelDTO);
        QueryWrapper<RankLevel> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( rankLevel, RankLevel.class );
        queryWrapper.orderByAsc("sort").orderByDesc("create_date");
        IPage<RankLevel> result = rankLevelService.page (page,queryWrapper);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询职层数据
     * @param id
     * @return
     */
    @ApiLog("查询职层详情")
    @PreAuthorize ("hasAnyAuthority('rank:rank_level:view','rank:rank_level:add','rank:rank_level:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        RankLevel rankLevel = rankLevelService.getById ( id );
        return ResponseEntity.ok (rankLevel);
    }

    /**
     * 添加职层
     * @param rankLevelDTO
     * @return
     */
    @ApiLog(value = "修改/新增职层", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('rank:rank_level:save','rank:rank_level:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody RankLevelDTO rankLevelDTO, HttpServletRequest request) {
        RankLevel rankLevel = RankLevelWrapper.INSTANCE.toEntity(rankLevelDTO);
        rankLevelService.saveOrUpdate(rankLevel);
        return ResponseEntity.ok ("保存职层成功");
    }

    /**
     * 删除职层
     * @param ids
     * @return
     */
    @ApiLog(value = "删除职层", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('rank:rank_level:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids,HttpServletRequest request) {
        String idArray[] =ids.split(",");
        rankLevelService.removeByIds (Lists.newArrayList (idArray));
        return ResponseEntity.ok ("删除职层成功");
    }




}
