package jnpf.personnels.controller.web.formfield;

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.database.util.TenantDataSourceUtil;
import jnpf.exception.LoginException;
import jnpf.model.personnels.po.FtbPersonnelsPromiseConfig;
import jnpf.personnels.service.FtbPersonnelsPromiseConfigService;
import jnpf.util.NoDataSourceBind;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * web本人承诺文案配置表
 *
 * @author peng.hao
 */
@RestController
@RequestMapping("/web/personnels-promise-config")
public class FtbPersonnelsPromiseConfigController {

    /**
     * 服务对象
     */
    @Resource
    private FtbPersonnelsPromiseConfigService ftbPersonnelsPromiseConfigService;

    /**
     * 查询文案
     * @return
     */
    @GetMapping("/query-list")
    public ActionResult<List<FtbPersonnelsPromiseConfig>> queryList() {
        LambdaQueryWrapper<FtbPersonnelsPromiseConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnelsPromiseConfig::getEnableMark,0);
        List<FtbPersonnelsPromiseConfig> list = ftbPersonnelsPromiseConfigService.list(wrapper);
        return ActionResult.success(list);
    }

    /**
     * 新增文案
     * @return
     */
    @PostMapping("/addNewCopy")
    public ActionResult<String> addNewCopy(@Validated @RequestBody List<FtbPersonnelsPromiseConfig> list) {
        if (list.size() > 30 ) return ActionResult.fail("最多只能保存30条数据");
        Set<String> stringSet = list.stream().map(FtbPersonnelsPromiseConfig::getCopyDetails).collect(Collectors.toSet());
        if (stringSet.size() != list.size()) return ActionResult.fail("添加文案中包含重复,请勿重复添加!");
        // 移除已有的
        List<FtbPersonnelsPromiseConfig> configList = list.stream().filter(item -> item.getIsBuiltIn() != null && item.getIsBuiltIn() != 1).collect(Collectors.toList());
        LambdaUpdateWrapper<FtbPersonnelsPromiseConfig> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbPersonnelsPromiseConfig::getIsBuiltIn,0);
        ftbPersonnelsPromiseConfigService.remove(updateWrapper);
        ftbPersonnelsPromiseConfigService.saveBatch(configList);
        // 内置的如果有直接更新
        ftbPersonnelsPromiseConfigService.updateBatchById(list.stream().filter(item -> item.getIsBuiltIn() != null && item.getIsBuiltIn() == 1).collect(Collectors.toList()));
        return ActionResult.success();
    }

    /**
     * 查询文案(外部)
     * @return
     */
    @GetMapping("/query-list-copy-writing")
    @NoDataSourceBind
    public ActionResult<List<FtbPersonnelsPromiseConfig>> queryListCopyWriting(@RequestParam("tenantId") String tenantId) throws LoginException {
        TenantDataSourceUtil.switchTenant(tenantId);
        LambdaQueryWrapper<FtbPersonnelsPromiseConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnelsPromiseConfig::getEnableMark,0);
        List<FtbPersonnelsPromiseConfig> list = ftbPersonnelsPromiseConfigService.list(wrapper);
        return ActionResult.success(list);
    }

    /**
     * 个人承若显隐设置状态查询(内部)
     *
     * @return 是否显示签字板及个人承诺内容(1是 0否)
     */
    @GetMapping("/query-list-copy-writing-status")
    public ActionResult<Integer> queryListCopyWritingStatus() {
        return getIntegerActionResult();
    }

    /**
     * 个人承若显隐设置状态查询(外部)
     *
     * @param tenantId 租户 ID
     * @return 是否显示签字板及个人承诺内容(1是 0否)
     */
    @GetMapping("/query-list-copy-writing-status-external")
    @NoDataSourceBind
    public ActionResult<Integer> queryListCopyWritingStatusExternal(@RequestParam("tenantId") String tenantId) throws LoginException {
        TenantDataSourceUtil.switchTenant(tenantId);
        return getIntegerActionResult();
    }

    @NotNull
    private ActionResult<Integer> getIntegerActionResult() {
        LambdaQueryWrapper<FtbPersonnelsPromiseConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(FtbPersonnelsPromiseConfig::getEnableMark, 0);
        wrapper.eq(FtbPersonnelsPromiseConfig::getIsBuiltIn, 1);
        wrapper.last("limit 1");
        FtbPersonnelsPromiseConfig promiseConfig = ftbPersonnelsPromiseConfigService.getOne(wrapper);
        return ActionResult.success(promiseConfig.getDisplay());
    }

    /**
     * 修改个人承若显隐设置状态
     *
     * @param display 是否显示签字板及个人承诺内容(1是 0否)
     */
    @PutMapping("/update-copy-writing-status/{display}")
    public ActionResult<Void> updateCopyWritingStatus(@PathVariable(value = "display") Integer display) {
        LambdaUpdateWrapper<FtbPersonnelsPromiseConfig> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(FtbPersonnelsPromiseConfig::getDisplay, display);
        ftbPersonnelsPromiseConfigService.update(updateWrapper);
        return ActionResult.success();
    }


}
