package org.springblade.modules.system.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.VisualSubscribeNotifyConf;
import org.springblade.modules.system.enums.NotifyType;
import org.springblade.modules.system.service.VisualSubscribeNotifyConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subscribeNotifyConf")
@Api(value = "订阅方式配置接口", tags = "订阅方式配置接口")
public class VisualSubscribeNotifyConfController extends BladeController {

    @Autowired
    private VisualSubscribeNotifyConfService notifyConfService;

    /**
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "订阅方式配置列表")
    public R<List<VisualSubscribeNotifyConf>> list(VisualSubscribeNotifyConf visualSubscribeNotifyConf){
        QueryWrapper<VisualSubscribeNotifyConf> queryWrapper = new QueryWrapper<VisualSubscribeNotifyConf>(visualSubscribeNotifyConf);
        queryWrapper.lambda().orderByAsc(VisualSubscribeNotifyConf::getTenantId).orderByAsc(VisualSubscribeNotifyConf::getSort);
        return R.data(notifyConfService.list(queryWrapper));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "保存 save", notes = "传入list")
    public R<Boolean> save(@RequestBody List<VisualSubscribeNotifyConf> list) {
        for (VisualSubscribeNotifyConf visualSubscribeNotifyConf : list) {
            // 验证人脸识别登录方式是否满足条件
            if (visualSubscribeNotifyConf.getNotifyType().getValue() == NotifyType.systemSetting.getValue() &&
                    "loginType".equals(visualSubscribeNotifyConf.getAttriKey()) &&
                    visualSubscribeNotifyConf.getAttriValue().contains("3")){
                long count = notifyConfService.count(Wrappers.<VisualSubscribeNotifyConf>lambdaQuery()
                        .eq(VisualSubscribeNotifyConf::getNotifyType, NotifyType.baiduAI.getValue()));
                if (count < 1){
                    return R.fail("系统未配置人脸识别相关信息...");
                }
            }
        }
        // 为登录方式提供默认值
        list.forEach(item -> {
            // 为登录方式赋默认值
            if (item.getNotifyType().getValue() == NotifyType.systemSetting.getValue() &&
                    "loginType".equals(item.getAttriKey()) &&
                    StrUtil.isEmpty(item.getAttriValue())){
                item.setAttriValue("1");
            }
        });

    	//清除缓存
    	list.forEach(item -> notifyConfService.evict(item.getNotifyType()));

        return R.data(notifyConfService.saveOrUpdateBatch(list));
    }

}
