package jnpf.personnels.controller.web.rule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.model.personnels.po.FtbPersonnelsRuleConfig;
import jnpf.model.personnels.po.FtbPersonnelsSecondmentConfig;
import jnpf.personnels.mapper.FtbPersonnelsSecondmentConfigMapper;
import jnpf.personnels.service.FtbPersonnelsRuleConfigService;
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/personnels_rule_config")
public class FtbPersonnelsRuleConfigController {
    /**
     * 服务对象
     */
    @Resource
    private FtbPersonnelsRuleConfigService ftbPersonnelsRuleConfigServiceImpl;


    @Resource
    private FtbPersonnelsSecondmentConfigMapper ftbPersonnelsSecondmentConfigMapper;

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public ActionResult<FtbPersonnelsRuleConfig> selectOne() {
        return ActionResult.success(ftbPersonnelsRuleConfigServiceImpl.query().one()) ;
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public ActionResult<Void> edit(@RequestBody FtbPersonnelsRuleConfig ftbPersonnelsRuleConfig) {
        ftbPersonnelsRuleConfigServiceImpl.updateById(ftbPersonnelsRuleConfig);
        return ActionResult.success();
    }

    /**
     * 规则查询借调薪资设置
     */
    @GetMapping("/queryInfo")
    public ActionResult<List<FtbPersonnelsSecondmentConfig>> queryInfo() {
        LambdaQueryWrapper<FtbPersonnelsSecondmentConfig> queryWrapper = Wrappers.lambdaQuery();
        List<FtbPersonnelsSecondmentConfig> ftbPersonnelsSecondmentConfigs = ftbPersonnelsSecondmentConfigMapper.selectList(queryWrapper);
        return ActionResult.success(ftbPersonnelsSecondmentConfigs);
    }
    /**
     * 编辑借调规则配置开启
     */
    @PutMapping("/editForOA")
    public ActionResult<Void> editForOA(@RequestParam String id) {
        // 将所有设置为关闭
        ftbPersonnelsSecondmentConfigMapper.update(new FtbPersonnelsSecondmentConfig(),
                new LambdaUpdateWrapper<FtbPersonnelsSecondmentConfig>().set(FtbPersonnelsSecondmentConfig::getIsOpen,0));
        LambdaUpdateWrapper<FtbPersonnelsSecondmentConfig> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(FtbPersonnelsSecondmentConfig::getIsOpen,1);
        wrapper.eq(FtbPersonnelsSecondmentConfig::getId,id);
        ftbPersonnelsSecondmentConfigMapper.update(new FtbPersonnelsSecondmentConfig(),wrapper);
        return ActionResult.success();
    }
    /**
     * 借调人员办理查询
     */
    @GetMapping("/queryInfoForOA")
    public ActionResult<List<FtbPersonnelsSecondmentConfig>> queryInfoForOA() {
        LambdaQueryWrapper<FtbPersonnelsSecondmentConfig> queryWrapper = Wrappers.lambdaQuery();
        List<FtbPersonnelsSecondmentConfig> secondmentConfigs = ftbPersonnelsSecondmentConfigMapper.selectList(queryWrapper);
        if (secondmentConfigs.stream().anyMatch(v -> v.getIsOpen() == 1 && v.getId().equals("3"))){
            return ActionResult.success(secondmentConfigs.stream().filter(v->!v.getId().equals("3")).collect(Collectors.toList()));
        }
        return ActionResult.success(secondmentConfigs.stream().filter(v->v.getIsOpen()==1).collect(Collectors.toList()));
    }

}
