package com.css.simulation.resource.server.adapter.controller.personal_information;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.scene.SystemScenePackageParam;
import api.common.pojo.po.scene.SystemScenePackageSublistPO;
import api.common.pojo.po.system.UserPO;
import api.common.pojo.vo.scene.SystemScenePackageVO;
import api.common.pojo.vo.system.ClusterVO;
import api.common.pojo.vo.system.ParameterVO;
import api.common.pojo.vo.system.UserVO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.SystemScenePackageSublistMapper;
import com.css.simulation.resource.server.app.service.SystemScenePackageService;
import com.css.simulation.resource.server.app.service.ClusterService;
import com.css.simulation.resource.server.app.service.ParameterService;
import com.css.simulation.resource.server.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.List;


@Controller()
@RequestMapping("/userInfo")
public class UserInfoController {


    @Autowired
    UserService userService;

    @Autowired
    ClusterService clusterService;

    @Autowired
    ParameterService parameterService;

    @Resource
    SystemScenePackageService systemScenePackageService;

    @Resource
    SystemScenePackageSublistMapper systemScenePackageSublistMapper;


    /**
     * 获取当前登录人详情
     */
    @RequestMapping("/getCurrentUserInfo")
    @ResponseBody
    public ResponseBodyVO getCurrentUserInfo() {
        ResponseBodyVO<UserVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(userService.getCurrentUserInfo());
        return response;
    }


    /**
     * 根据 user_id 查询参数配置信息
     */
    @RequestMapping("/getParameterByUserId")
    @ResponseBody
    public ResponseBodyVO<ParameterVO> getParameterByUserId() {
        ResponseBodyVO<ParameterVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        ParameterVO param = parameterService.getParameterByUserId();
        response.setInfo(param);
        return response;
    }

    /**
     * 根据user_id查询集群配置信息
     */
    @RequestMapping("/getClusterByUserId")
    @ResponseBody
    public ResponseBodyVO<ClusterVO> getClusterByUserId() {
        ResponseBodyVO<ClusterVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        ClusterVO clusterVO = clusterService.getClusterByUserId();
        response.setInfo(clusterVO);
        return response;
    }

    /**
     * 获取场景资源
     */
    @RequestMapping("/getSceneResource")
    @ResponseBody
    public ResponseBodyVO getSceneResource() {
        String roleCode = AuthUtil.getCurrentUserRoleCode();
        String userId = AuthUtil.getCurrentUserId();
        if(DictConstants.ROLE_CODE_ADMIN.equals(roleCode) || DictConstants.ROLE_CODE_SYSADMIN.equals(roleCode)){ //管理员账户
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        }else if(DictConstants.ROLE_CODE_SUBUESR.equals(roleCode)){
            //查询父账户
            userId = AuthUtil.getCreateUserId();
        }
        SystemScenePackageParam param = new SystemScenePackageParam();
        param.setUserId(userId);
        List<SystemScenePackageVO> list = systemScenePackageService.querySystemScenePackageList(param);

        for (SystemScenePackageVO vo:list) {

            SystemScenePackageSublistPO sublistPo=new SystemScenePackageSublistPO();
            sublistPo.setSceneAndPackage(vo.getId());
            sublistPo.setSceneType("1");
            List<String>  zrList=systemScenePackageSublistMapper.querySceneNaturalList(sublistPo);
            sublistPo.setSceneType("2");
            List<String>  bzList=systemScenePackageSublistMapper.querySceneStandardsRegulationsList(sublistPo);
            sublistPo.setSceneType("3");
            List<String>  jtList=systemScenePackageSublistMapper.querySceneAccidentList(sublistPo);
            sublistPo.setSceneType("4");
            List<String>  fhList=systemScenePackageSublistMapper.querySceneGeneralTemplateList(sublistPo);
            vo.setZrCount(zrList.size());
            vo.setBzCount(bzList.size());
            vo.setJtCount(jtList.size());
            vo.setFhCount(fhList.size());
            vo.setTotalCount(zrList.size() + bzList.size() + jtList.size() + fhList.size());
        }


        long zrTotalCount = list.stream().mapToLong(SystemScenePackageVO::getZrCount).sum();
        long bzTotalCount = list.stream().mapToLong(SystemScenePackageVO::getBzCount).sum();
        long jtTotalCount = list.stream().mapToLong(SystemScenePackageVO::getJtCount).sum();
        long fhTotalCount = list.stream().mapToLong(SystemScenePackageVO::getFhCount).sum();
        long sum = list.stream().mapToLong(SystemScenePackageVO::getTotalCount).sum();
        SystemScenePackageVO last = new SystemScenePackageVO();
        last.setPackageName("合计");
        last.setZrCount((int)zrTotalCount);
        last.setBzCount((int)bzTotalCount);
        last.setFhCount((int)fhTotalCount);
        last.setJtCount((int)jtTotalCount);
        last.setTotalCount((int)sum);
        list.add(last);
        ResponseBodyVO<List> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(list);
        return response;
    }

    /**
     * 个人信息管理-修改联系方式
     */
    @RequestMapping("/savePhone")
    @ResponseBody
    public ResponseBodyVO savePhone(@RequestBody UserPO userPO) {
        if(ObjectUtil.isNull(userPO)){
            return new ResponseBodyVO(false, 400, "参数必传！",null);
        }
        if(ObjectUtil.isNull(userPO.getId()) || ObjectUtil.isNull(userPO.getNickname())|| ObjectUtil.isNull(userPO.getPhone())){
            return new ResponseBodyVO(false, 400, "参数必传！",null);
        }
        userService.savePhone(userPO);
        return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
    }

    /**
     * 个人信息管理-修改密码
     */
    @RequestMapping("/savePassword")
    @ResponseBody
    public ResponseBodyVO savePassword(@RequestBody UserVO userVO) throws NoSuchAlgorithmException {
        if(ObjectUtil.isNull(userVO)){
            return new ResponseBodyVO(false, 400, "参数必传！",null);
        }
        if(ObjectUtil.isNull(userVO.getId()) || ObjectUtil.isNull(userVO.getPassword()) || ObjectUtil.isNull(userVO.getNewPassword())){
            return new ResponseBodyVO(false, 400, "参数必传！",null);
        }
        UserPO userPORes = userService.savePassword(userVO);
        if(ObjectUtil.isNull(userPORes)){
            return new ResponseBodyVO(false, 400, "原密码错误！",null);
        }
        return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
    }



}
