/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.xrtygis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.xrtygis.entity.TfKpiStandardManage;
import com.tofly.xrtygis.service.TfKpiStandardManageService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;
import java.util.List;

/**
 * 绩效考核标准管理
 *
 * @author JF
 * @date Fri Mar 25 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tfkpistandardmanage")
@Api(tags="绩效考核标准管理接口")
public class TfKpiStandardManageController {

    private final  TfKpiStandardManageService tfKpiStandardManageService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param tfKpiStandardManage 绩效考核标准管理
    * @return
    */
    @GetMapping("/page" )
    @ToFlyAppLog(title="分页查询" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getTfKpiStandardManagePage(Page page, @ModelAttribute TfKpiStandardManage tfKpiStandardManage) {
        try{
            boolean type = tfKpiStandardManage.getAssessType() != null;
            boolean content = tfKpiStandardManage.getAssessContent() != null;
            return ResultRespone.success(tfKpiStandardManageService.page(page, Wrappers.query(tfKpiStandardManage)
                    .eq(type,"ASSESS_TYPE",tfKpiStandardManage.getAssessType())
                    .eq(content,"ASSESS_CONTENT",tfKpiStandardManage.getAssessContent())));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }

    }


    /**
     * 通过id查询绩效考核标准管理
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ToFlyAppLog(title="通过ID查询" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
        try{
            return ResultRespone.success(tfKpiStandardManageService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }

    }

    /**
     * 新增绩效考核标准管理
     * @param tfKpiStandardManage 绩效考核标准管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增绩效考核标准管理" )
    @ApiOperation(value = "新增绩效考核标准管理")
    @PostMapping
    public ResultRespone save(@RequestBody TfKpiStandardManage tfKpiStandardManage) {
        try{
            QueryWrapper<TfKpiStandardManage> queryWrapper = new QueryWrapper<>();
            boolean hasAssessContent = tfKpiStandardManage.getAssessContent() != null;
            List<TfKpiStandardManage> tfKpiStandardManageList = tfKpiStandardManageService.list(queryWrapper.eq(hasAssessContent,"ASSESS_CONTENT",tfKpiStandardManage.getAssessContent()));
            if(tfKpiStandardManageList.size()==2){
                return ResultRespone.failed("此考核类型已存在");
            }else if(tfKpiStandardManageList.size()==1){
                for(TfKpiStandardManage tfKpiStandardManageOne:tfKpiStandardManageList){
                    if(tfKpiStandardManageOne.getAssessType().equals(tfKpiStandardManage.getAssessType())){
                        return ResultRespone.failed("此考核时间状态已存在");
                    }
                }
            }
            return ResultRespone.success(tfKpiStandardManageService.save(tfKpiStandardManage));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }

    }

    /**
     * 修改绩效考核标准管理
     * @param tfKpiStandardManage 绩效考核标准管理
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改绩效考核标准管理" )
    @ApiOperation(value = "修改绩效考核标准管理")
    @PutMapping
    public ResultRespone updateById(@RequestBody TfKpiStandardManage tfKpiStandardManage) {
        try{
            return ResultRespone.success(tfKpiStandardManageService.updateById(tfKpiStandardManage));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }

    }

    /**
     * 通过id删除绩效考核标准管理
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除绩效考核标准管理" )
    @ApiOperation(value = "通过ID删除绩效考核标准管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
        try{
            return ResultRespone.success(tfKpiStandardManageService.removeById(id));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }

    }

    /**
     * 通过id批量删除绩效考核标准管理
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除绩效考核标准管理" )
    @ApiOperation(value = "通过ID批量删除绩效考核标准管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        try{
            return ResultRespone.success(tfKpiStandardManageService.removeByIds(Arrays.asList(ids.split(","))));
        }catch (Exception e){
            e.printStackTrace();
            return ResultRespone.failed();
        }

    }
}
