package jnpf.personnels.controller.web.resignation;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.base.vo.PageListVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.personnels.dto.resignation.FtbResignationConfigurationCategoryDTO;
import jnpf.model.personnels.po.FtbPersonnelsResignationCategoryConfiguration;
import jnpf.model.personnels.po.FtbPersonnelsResignationConfiguration;
import jnpf.model.personnels.vo.resignation.FtbResignationConfigurationCategoryVO;
import jnpf.personnels.service.FtbPersonnelsResignationCategoryConfigurationService;
import jnpf.personnels.service.FtbPersonnelsResignationConfigurationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * web离职原因类别
 * @Author:peng.hao
 */
@RestController
@RequestMapping("/web/resignation-category-config")
public class FtbResignationConfigurationCategoryController {


    @Resource
    FtbPersonnelsResignationCategoryConfigurationService service;
     @Resource

    private FtbPersonnelsResignationConfigurationService configurationService;

    /**
     * 离职原因类别配置列表
     *
     */
    @GetMapping("/list")
    public ActionResult<PageListVO<FtbResignationConfigurationCategoryVO>> getList(CultivatePage page) {
        Page<FtbResignationConfigurationCategoryVO> objectPage = page.coverCultivatePage();
        LambdaQueryWrapper<FtbPersonnelsResignationCategoryConfiguration> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(SuperBaseEntity.SuperCBaseEntity::getCreatorTime);
        List<FtbPersonnelsResignationCategoryConfiguration> list = service.list(wrapper);
        return ActionResult.success(CultivatePage.paginate(list.stream().map(FtbResignationConfigurationCategoryVO::covert).collect(Collectors.toList()),objectPage));
    }


    /**
     * 离职原因类别配置保存编辑
     *
     */
    @PostMapping("/configuration-save")
    public ActionResult<Void> saveOrUpdate(@Validated @RequestBody List<FtbResignationConfigurationCategoryDTO> categoryDTOS) {
        List<String> names = categoryDTOS.stream().map(FtbResignationConfigurationCategoryDTO::getResignationTypeName).collect(Collectors.toList());
        List<String> configIds = categoryDTOS.stream().filter(vo -> StringUtils.isNotEmpty(vo.getId())).map(FtbResignationConfigurationCategoryDTO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<FtbPersonnelsResignationCategoryConfiguration> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbPersonnelsResignationCategoryConfiguration::getResignationTypeName, names);
        wrapper.notIn(CollUtil.isNotEmpty(configIds), SuperBaseEntity.SuperIBaseEntity::getId,configIds);
        long l = service.count(wrapper);
        if (l > 0) throw new RuntimeException("离职原因分类名称已经存在,请勿重复添加!");
        service.saveOrUpdateBatch(categoryDTOS.stream().map(FtbResignationConfigurationCategoryDTO::covert).collect(Collectors.toList()));
        return ActionResult.success();
    }

    /**
     * 离职原因配置删除
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<Void> delete(@PathVariable("id") String id) {
        LambdaQueryWrapper<FtbPersonnelsResignationConfiguration> q = Wrappers.lambdaQuery();
        q.eq(FtbPersonnelsResignationConfiguration::getResignationTypeId,id);
        long count = configurationService.count(q);
        if (count > 0) return ActionResult.fail("该离职原因分类下有离职原因，请先删除其离职原因!");
        service.removeById(id);
        return ActionResult.success();
    }

}
