package com.xjrsoft.module.student.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.student.dto.AddCadreDeptDto;
import com.xjrsoft.module.student.dto.CadreDeptPageDto;
import com.xjrsoft.module.student.dto.CadreDeptTreeDto;
import com.xjrsoft.module.student.dto.UpdateCadreDeptDto;
import com.xjrsoft.module.student.entity.CadreDept;
import com.xjrsoft.module.student.service.ICadreDeptService;
import com.xjrsoft.module.student.vo.CadreDeptPageVo;
import com.xjrsoft.module.student.vo.CadreDeptTreeVo;
import com.xjrsoft.module.student.vo.CadreDeptVo;
import com.xjrsoft.module.teacher.entity.XjrUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 干部部门
* @Author szs
* @Date: 2023-12-20
* @Version 1.0
*/
@RestController
@RequestMapping("/student" + "/cadreDept")
@Api(value = "/student"  + "/cadreDept",tags = "干部部门代码")
@AllArgsConstructor
public class CadreDeptController {


    private final ICadreDeptService cadreDeptService;

    @GetMapping(value = "/page")
    @ApiOperation(value="干部部门列表(分页)")
    @SaCheckPermission("cadredept:detail")
    public RT<PageOutput<CadreDeptPageVo>> page(@Valid CadreDeptPageDto dto){

        MPJLambdaWrapper<CadreDept> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .selectAs(XjrUser::getName, CadreDeptTreeVo::getInstructorUserIdCN)
                .select(CadreDept.class, x -> VoToColumnUtil.fieldsToColumns(CadreDeptPageVo.class).contains(x.getProperty()))
                .leftJoin(XjrUser.class, XjrUser::getId, CadreDept::getInstructorUserId)
                .orderByDesc(CadreDept::getId);
        IPage<CadreDeptPageVo> page = cadreDeptService.selectJoinListPage(ConventPage.getPage(dto),CadreDeptPageVo.class, queryWrapper);
        PageOutput<CadreDeptPageVo> pageOutput = ConventPage.getPageOutput(page, CadreDeptPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/tree")
    @ApiOperation(value = "干部部门树")
    @SaCheckPermission("cadredept:detail")
    public RT<List<CadreDeptTreeVo>> tree(CadreDeptTreeDto dto) {
        List<CadreDeptTreeVo> treeVoList = cadreDeptService.getCadreDeptTree(dto);
        return RT.ok(treeVoList);
    }

    @PutMapping("/updateInstructorUser")
    @ApiOperation(value = "部门(添加/修改)部门指导教师用户编号")
    @SaCheckPermission("cadredept:edit")
    public RT<Boolean> updateInstructorUser(@Valid @RequestBody UpdateCadreDeptDto dto){
        CadreDept cadreDept = BeanUtil.toBean(dto, CadreDept.class);
        return RT.ok(cadreDeptService.updateById(cadreDept));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询干部部门信息")
    @SaCheckPermission("cadredept:detail")
    public RT<CadreDeptVo> info(@RequestParam Long id){
        CadreDept cadreDept = cadreDeptService.getById(id);
        if (cadreDept == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(cadreDept, CadreDeptVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增干部部门")
    @SaCheckPermission("cadredept:add")
    public RT<Boolean> add(@Valid @RequestBody AddCadreDeptDto dto){
        //校验code唯一性
        if(ObjectUtil.isNotNull(dto.getCode())){
            CadreDept cadreDept = cadreDeptService.getOne(Wrappers.<CadreDept>lambdaQuery()
                    .eq(CadreDept::getCode,dto.getCode()));
            if(ObjectUtil.isNotNull(cadreDept)){
                throw new MyException("干部部门编号不唯一");
            }
        }
        CadreDept cadreDept = BeanUtil.toBean(dto, CadreDept.class);
        boolean isSuccess = cadreDeptService.save(cadreDept);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改干部部门")
    @SaCheckPermission("cadredept:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateCadreDeptDto dto){
        //校验code唯一性
        if(ObjectUtil.isNotNull(dto.getCode())){
            CadreDept cadreDept = cadreDeptService.getOne(Wrappers.<CadreDept>lambdaQuery()
                    .eq(CadreDept::getCode,dto.getCode()));
            if(ObjectUtil.isNotNull(cadreDept) && cadreDept.getId() != (dto.getId())){
                throw new MyException("干部部门编号不唯一");
            }
        }
        CadreDept cadreDept = BeanUtil.toBean(dto, CadreDept.class);
        return RT.ok(cadreDeptService.updateById(cadreDept));
    }

    @DeleteMapping
    @ApiOperation(value = "删除干部部门")
    @SaCheckPermission("cadredept:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(cadreDeptService.removeBatchByIds(ids));

    }

}