package com.rf.psychological.group.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.opLog.annotation.OperationLogAnnotation;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.group.service.GroupAuthService;
import com.rf.psychological.group.service.GroupInfoService;
import com.rf.psychological.institution.service.InstitutionAuthService;
import com.rf.psychological.utils.Result;
import com.rf.psychological.dao.dto.InstitutionAuthConTaskDto;
import com.rf.psychological.dao.dto.InstitutionAuthSubjectNameDto;
import com.rf.psychological.group.dao.model.GroupAuthEntity;
import com.rf.psychological.institution.model.InstitutionAuthEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lpf
 * @description:
 * @date 26/10/2021下午4:44
 */

@Slf4j
@RestController
@RequestMapping("/group/auth")
@Api(tags = "组权限操作")
public class GroupAuthController extends BaseController {

    @Autowired
    private GroupInfoService groupInfoService;

    @Autowired
    private GroupAuthService groupAuthService;

    @Autowired
    private InstitutionAuthService institutionAuthService;
    @SafetyProcess
    @GetMapping("/find")
    @ApiOperation("查找组的权限")
    public Result findGroup(@RequestParam("groupId") String groupId,
                            @RequestParam("type") String type, @RequestParam("searchKey") String name,
                            @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        try {
                //查询量表权限
                if (type.equals("0")) {
                    return success(this.groupAuthService.findGroupAuth(groupId, pageNum, pageSize, name));
                } else {
                    //查询认知任务权限
                    return success(this.groupAuthService.findGroupTaskAuth(groupId, pageNum, pageSize, name));
                }
        } catch (Exception exception) {
            exception.printStackTrace();
            return fail("", "find error，" + exception.toString());
        }
    }

    @SafetyProcess
    @GetMapping("/findNoAuth")
    @ApiOperation("查找组没有的权限")
    public Result findGroupNoAuth(@RequestParam("groupId") String groupId, @RequestParam("institutionNo") String institutionNo,
                                  @RequestParam("type") String type, @RequestParam("searchKey") String searchKey,
                                  @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        try {
            JSONObject result = new JSONObject();
            //查询量表部分
            if (type.equals("0")) {
                Page<InstitutionAuthSubjectNameDto> groupNoAuthList = this.groupAuthService.findGroupNoAuthList(institutionNo, groupId, searchKey, pageNum, pageSize);
                result.put("data", groupNoAuthList.getContent());
                result.put("allNum", groupNoAuthList.getTotalElements());
            } else {
                Page<InstitutionAuthConTaskDto> groupNoAuthCoTaskList = this.groupAuthService.findGroupNoAuthCoTaskList(institutionNo, groupId, searchKey, pageNum, pageSize);
                result.put("data", groupNoAuthCoTaskList.getContent());
                result.put("allNum", groupNoAuthCoTaskList.getTotalElements());
            }
            return success(result);
        } catch (Exception exception) {
            exception.printStackTrace();
            return fail("", "find error，" + exception.toString());
        }
    }

    @SafetyProcess
    @OperationLogAnnotation("删除组权限")
    @PostMapping("/del")
    @ApiOperation("根据组权限的id删除组权限ids")
    public Result delGroup(@ApiParam("传入一个id的list类型，每个String") @RequestBody String json) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        System.out.println("data=====:" + data);
        JSONObject jsonObject = JSONArray.parseObject(data);

//        List<String> ids = (List<String>) jsonObject.get("ids");
        Object idObs = jsonObject.get("ids");
        List<String> ids = new ArrayList<>();
        for (Object o : (List<?>) idObs) {
            ids.add((String) o);
        }
        ids.forEach(System.out::println);
        try {
            //删除组权限
            this.groupAuthService.deleteAuthBath(ids);
            return success();
        } catch (Exception exception) {
            exception.printStackTrace();
            return fail("", "error，" + exception.toString());
        }
    }


    @SafetyProcess
    @OperationLogAnnotation
    @PostMapping("/add")
    @ApiOperation("添加组权限")
    public Result addGroupAuth(@RequestBody String json) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        System.out.println("data=====:" + data);
        JSONArray jsonObject = JSONArray.parseArray(data);

        try {
            List<InstitutionAuthEntity> authList = new ArrayList<>();
            for (int i = 0; i < jsonObject.size(); i++) {
                JSONObject groupAuObj = (JSONObject) jsonObject.get(i);
                GroupAuthEntity groupAuthEntity = groupAuObj.toJavaObject(GroupAuthEntity.class);
                if (("").equals(groupAuthEntity.getGroupId()) || ("").equals(groupAuthEntity.getType()) || ("").equals(groupAuthEntity.getBId())) {
                    return fail("数据存在空值");
                }
                log.info("groupAuthEntity" + groupAuthEntity);
                this.groupAuthService.addGroupAuth(groupAuthEntity);
            }
            return success();
        } catch (Exception exception) {
            exception.printStackTrace();
            return fail("", "error，" + exception.toString());
        }
    }
}
