package com.storlead.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.result.Result;
import com.storlead.system.util.SystemSettingCacheCheckUtil;
import com.storlead.user.pojo.dto.JobDTO;
import com.storlead.user.pojo.entity.JobEntity;
import com.storlead.user.service.IJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @program: sp-cloud
 * @description:
 * @author: chenkq
 * @create: 2022-05-24 11:03
 */
@Api(tags = "平台中心 - 系统: 角色管理")
@RestController
@RequestMapping("/sys/job/manage")
public class JobApiController {

    @Resource
    private IJobService jobService;

    @ApiOperation("岗位分页查询")
    @PostMapping("/listpage")
    public Result<?> listpage(@RequestBody JobDTO param) {
        IPage<JobEntity> page = new Page<>(param.getPageIndex(),param.getPageSize());
        LambdaQueryWrapper<JobEntity> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(param.getJobName())) {
            queryWrapper.like(JobEntity::getName,param.getJobName());
        }
        if (Objects.nonNull(param.getId())) {
            queryWrapper.eq(JobEntity::getId,param.getId());
        }
        queryWrapper.eq(JobEntity::getIsDelete,0);
        queryWrapper.eq(JobEntity::getEnabled,1);
        queryWrapper.orderByAsc(JobEntity::getSort);
        IPage<JobEntity> jobs = jobService.page(page,queryWrapper);
        return Result.result(jobs);
    }

    @ApiOperation("岗位查询")
    @PostMapping("/listquery")
    public Result<?> listquery(@RequestBody JobDTO param) {
        LambdaQueryWrapper<JobEntity> queryWrapper = new LambdaQueryWrapper();
        if (StrUtil.isNotBlank(param.getJobName())) {
            queryWrapper.like(JobEntity::getName,param.getJobName());
        }
        queryWrapper.eq(JobEntity::getIsDelete,0);
        queryWrapper.eq(JobEntity::getEnabled,1);
        queryWrapper.orderByAsc(JobEntity::getSort);
        List<JobEntity> jobs = jobService.list(queryWrapper);
        return Result.result(jobs);
    }

    @ApiOperation("保存岗位")
    @PostMapping("/save")
    public Result create(@RequestBody JobEntity info) {
        Result r = SystemSettingCacheCheckUtil.checkAddDataMode();
        if (!r.isSuccess()) {
            return r;
        }
        if (Objects.isNull(info) || StrUtil.isBlank(info.getName())) {
            return Result.error("参数错误");
        }
        if(Objects.isNull(info.getId())) {
            info.setDataSource(1);
        }
        if (Objects.isNull(info.getId())) {
            Integer exit = jobService.count(new LambdaQueryWrapper<JobEntity>().eq(JobEntity::getName,info.getName()));
            if (exit > 0) {
                return Result.error("岗位'"+ info.getName() +"'已存在!");
            }
        } else {
            JobEntity oldAccount = jobService.getOne(new LambdaQueryWrapper<JobEntity>().eq(JobEntity::getName,info.getName()));
            if (Objects.nonNull(oldAccount)) {
                if (!oldAccount.getId().equals(info.getId())) {
                    return Result.error("岗位'"+ info.getName() +"'已存在,无法修改!");
                }
            }
        }
        jobService.saveOrUpdate(info);
        return Result.ok();
    }

    @ApiOperation("删除岗位")
    @PostMapping("/delete")
    public Result<?> remove(Long [] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            return Result.error("参数错误");
        }
        jobService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }
}
