package com.template.controller;


import com.template.api.SmartApplyControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartApply;
import com.template.model.pojo.SmartIdentity;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.ApplyVo;
import com.template.model.vo.ApplysVo;
import com.template.services.SmartApplyService;
import com.template.services.SmartIdentityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@RestController
public class SmartApplyController implements SmartApplyControllerAPI {

    @Autowired
    private SmartApplyService smartApplyService;

    @Autowired
    private SmartIdentityService smartIdentityService;

    /**
     * 查询应用管理
     *
     * @return
     */
    @Override
    public CommonResult queryApplys() {
        List<ApplyVo> result = smartApplyService.queryApplys();
        return CommonResult.ok(result);
    }

    /**
     * 新增应用管理
     *
     * @param smartApply    应用管理数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartApply(SmartApply smartApply, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartApply data = smartApplyService.queryApplyByName(smartApply.getName());
        if (data != null) {
            return CommonResult.fail("该应用管理已存在");
        }


        int result = smartApplyService.insertSmartApply(smartApply);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 更新应用管理
     *
     * @param sa            应用管理数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartApplyById(SmartApply sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartApply oldData = smartApplyService.getSmartById(sa.getId());
        if (oldData == null) {
            return CommonResult.fail("应用管理数据无效，修改失败");
        }

        if (sa.getName() != null && !sa.getName().equals(oldData.getName())) {
            //查看是否存在相同身份的数据
            SmartApply data = smartApplyService.queryApplyByName(sa.getName());
            if (data != null) {
                return CommonResult.fail("该应用管理已存在");
            }
        }

        int result = smartApplyService.updateSmartApply(sa);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 应用管理分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartApplys(int currentPage, int pageCount, String name) {
        PageUtils<SmartApply> result = smartApplyService.queryPageSmartApplys(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }


    @Override
    public CommonResult deleteSmartApplyById(int id) {

        SmartApply data = smartApplyService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        //查看被删除的应用管理有没有绑定身份数据 有的话提示无法删除
        List<SmartIdentity> identitys = smartIdentityService.querySmartIdentityDatas(id);
        if (identitys != null && identitys.size() > 0) {
            String name = StringUtils.join(identitys.stream().map(SmartIdentity::getName).collect(Collectors.toList()), ',');
            return CommonResult.fail("当前应用数据已绑定" + name + "身份，请先解绑");
        }

        int result = smartApplyService.deleteSmartApplyById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    @Override
    public CommonResult queryAppletApplys() {
        List<ApplysVo> result = new ArrayList<>();

        List<SmartApply> applys = smartApplyService.queryAppletApplys();
        for (SmartApply apply : applys) {
            ApplysVo data = new ApplysVo();
            data.setId(apply.getId());
            data.setPath(apply.getRoute());
            data.setTitle(apply.getName());
            data.setUrl(apply.getLogo());
            result.add(data);
        }

        return CommonResult.ok(result);
    }
}

