package com.template.controller;


import com.template.api.SmartIdentityControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartApply;
import com.template.model.pojo.SmartIdentity;
import com.template.model.pojo.SmartScreenshot;
import com.template.model.request.insertIdentityRequest;
import com.template.model.request.updateIdentityRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.ApplysVo;
import com.template.model.vo.IdentityApplyVo;
import com.template.model.vo.SemesterVo;
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.RestController;

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

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

    @Autowired
    private SmartApplyService smartApplyService;

    @Autowired
    private SmartIdentityService smartIdentityService;

    /**
     * 新增身份
     *
     * @param smartIdentity 身份数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartIdentity(insertIdentityRequest smartIdentity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartIdentity data = smartIdentityService.queryIdentityByName(smartIdentity.getName());
        if (data != null) {
            return CommonResult.fail("该身份已存在");
        }

        SmartIdentity identity = new SmartIdentity();
        identity.setName(smartIdentity.getName());
        identity.setApplyId(StringUtils.join(smartIdentity.getApplyIds(), ','));
        int result = smartIdentityService.insertSmartIdentity(identity);

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

    /**
     * 更新身份
     *
     * @param sa            身份数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartIdentityById(updateIdentityRequest sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        SmartIdentity oldData = smartIdentityService.getSmartById(sa.getId());
        if (oldData == null) {
            return CommonResult.fail("身份数据无效，修改失败");
        }

        if (sa.getName() != null && !sa.getName().equals(oldData.getName())) {
            //查看是否存在相同身份的数据
            SmartIdentity data = smartIdentityService.queryIdentityByName(sa.getName());
            if (data != null) {
                return CommonResult.fail("该身份已存在");
            }
        }

        SmartIdentity identity = new SmartIdentity();
        identity.setId(sa.getId());
        identity.setName(sa.getName());
        identity.setApplyId(StringUtils.join(sa.getApplyIds(), ','));

        int result = smartIdentityService.updateSmartIdentity(identity);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 身份分页数据查询
     *
     * @param currentPage 当前页数
     * @param pageCount   一页数据条数
     * @param name        查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartIdentitys(int currentPage, int pageCount, String name) {
        PageUtils<SmartIdentity> result = smartIdentityService.queryPageSmartIdentitys(currentPage, pageCount, name);

        for (SmartIdentity data : result.getList()) {
            List<String> lists = Arrays.asList(data.getApplyId().split(","));
            data.setApplyIds(lists);
        }

        return CommonResult.ok(result);
    }


    @Override
    public CommonResult deleteSmartIdentityById(int id) {

        SmartIdentity data = smartIdentityService.getSmartById(id);

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

        int result = smartIdentityService.deleteSmartIdentityById(id);

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

    @Override
    public CommonResult querySmartIdentitys() {

        List<SmartIdentity> datas = smartIdentityService.list(null);

        List<SemesterVo> result = new ArrayList<>();
        for (SmartIdentity data : datas) {
            SemesterVo model = new SemesterVo();
            model.setId(data.getId());
            model.setName(data.getName());
            result.add(model);
        }

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryIdentityApplyById(int id) {

        List<ApplysVo> result = new ArrayList<>();

        SmartIdentity identity = smartIdentityService.getSmartById(id);
        if (identity == null) {
            return CommonResult.fail("当前身份不合法，无应用权限");
        }

        if (identity.getApplyId() == null) {
            return CommonResult.fail("当前身份信息无应用权限");
        }

        List<SmartApply> applys = smartApplyService.queryAppletApplys();
        if(applys == null){
            return CommonResult.fail("应用管理数据为空");
        }

        List<String> applyIds = Arrays.asList(identity.getApplyId().split(","));
        for (String applyId : applyIds) {
            Optional<SmartApply> applyData = applys.stream().filter(e -> e.getId().equals(Integer.valueOf(applyId))).findFirst();
            if(applyData != null && applyData.isPresent()){
                ApplysVo data = new ApplysVo();
                data.setId(applyData.get().getId());
                data.setPath(applyData.get().getRoute());
                data.setTitle(applyData.get().getName());
                data.setUrl(applyData.get().getLogo());
                data.setAppid(applyData.get().getAppid());
                data.setCategory(applyData.get().getCategory());
                result.add(data);
            }
        }

        return CommonResult.ok(result);
    }
}

