package com.jpsoft.shinestar.modules.mobile.controller;

import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author 墨鱼_mo
 * @date 2020-10-13 15:58
 */
@RestController
@RequestMapping("/mobile/scene")
@Slf4j
public class SceneInfoApiController {

    @Autowired
    private SceneInfoService sceneInfoService;

    @Autowired
    private RegionInfoService regionInfoService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonRoleInfoService personRoleInfoService;

    @Autowired
    private PersonRoleIconService personRoleIconService;

    @Autowired
    private CompanyStructureInfoService companyStructureInfoService;

    @Autowired
    private CompanyPositionService companyPositionService;


    @PostMapping("sceneList")
    @ApiOperation(value = "场景列表（公共接口）")
    public MessageResult<Map> sceneList() {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            List<SceneInfo> list = sceneInfoService.list();
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);

            messageResult.setData(map);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("regionList")
    @ApiOperation(value = "区域列表（公共接口）")
    public MessageResult<List> regionList(String regionName) {
        MessageResult<List> messageResult = new MessageResult<>();

        try {


            List<Map<String, Object>> listMap = new ArrayList<>();
            List<RegionInfo> regionInfoList = regionInfoService.findTopRegion();
            if (regionInfoList.size() > 0) {
                for (RegionInfo regionInfo : regionInfoList) {
                    Map<String, Object> map = new HashMap<>();
                    if (StringUtils.isNotBlank(regionName) && regionName.equals(regionInfo.getName())) {
                        List<RegionInfo> regionInfoList1 = regionInfoService.findByParentId(regionInfo.getId());
                        map.put("name", regionName);
                        map.put("list", regionInfoList1);
                        listMap.add(map);
                    } else {
                        map.put("name", regionInfo.getName());
                        map.put("list", new ArrayList<>());
                        listMap.add(map);
                    }
                    //     listMap.add(map);
                }
            }

            messageResult.setData(listMap);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("companyListByRegion")
    @ApiOperation(value = "按照区域获取公司列表（公共接口）")
    public MessageResult<Map> companyListByRegion(String regionId, String sceneId) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {


            List<Map<String, Object>> listMap = new ArrayList<>();
            RegionInfo regionInfo = regionInfoService.get(regionId);
            if (regionInfo == null) {
                throw new Exception("区域不存在");
            }
            if ("4".equals(sceneId)){
                sceneId = "2";
            }

            List<CompanyInfo> companyInfoList = companyInfoService.findByRegionIdAndSceneIdAndEnableRegister(regionId, sceneId,true);

            /*if ("1".equals(sceneId)){
                companyInfoList = companyInfoService.findByRegionIdAndSceneIdAndEnableRegister(regionId, sceneId,true);
            }else {
                companyInfoList = companyInfoService.findByRegionIdAndSceneId(regionId, sceneId);
            }*/
         //   List<CompanyInfo> companyInfoList = companyInfoService.findByRegionIdAndSceneId(regionId, sceneId);
            Map<String, Object> map = new HashMap<>();
            map.put("name", regionInfo.getName());
            map.put("list", companyInfoList);

            messageResult.setData(map);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }


    @PostMapping("personRoleListByScene")
    @ApiOperation(value = "按照场景查询用户角色列表（公共接口）")
    public MessageResult<List> personRoleListByScene(String sceneId) {
        MessageResult<List> messageResult = new MessageResult<>();

        try {


            List<Map<String, Object>> listMap = new ArrayList<>();
            List<PersonRoleInfo> personRoleInfoList = personRoleInfoService.findBySceneId(sceneId);
            if (personRoleInfoList.size() > 0) {

                for (PersonRoleInfo personRoleInfo : personRoleInfoList) {
                    if (personRoleInfo.getRegisterEnable()) {


                        Map<String, Object> map = new HashMap<>();
                        List<PersonRoleIcon> personRoleIconList = personRoleIconService.findByPersonRoleId(personRoleInfo.getId());
                        String remark = "";
                        if (personRoleIconList.size() > 0) {

                            for (PersonRoleIcon personRoleIcon : personRoleIconList) {
                                if (StringUtils.isBlank(remark)) {
                                    remark = personRoleIcon.getIconName();
                                } else {
                                    remark = remark + "、" + personRoleIcon.getIconName();
                                }
                            }

                        }
                        map.put("id", personRoleInfo.getId());
                        map.put("name", personRoleInfo.getPersonRoleName());
                        map.put("pic", personRoleInfo.getPic());
                        map.put("remark", remark);
                        listMap.add(map);

                    }
                }
            }


            messageResult.setData(listMap);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("companyStructureList")
    @ApiOperation(value = "按照公司id查询结构列表（公共接口）")
    public MessageResult<Map> companyStructureList(String companyId) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {


            CompanyInfo companyInfo = companyInfoService.get(companyId);
            List<CompanyStructureInfo> companyStructureInfoList = companyStructureInfoService.findTopStructureByCompanyId(companyId);

            List<CompanyPosition> companyPositionList = companyPositionService.findByCompanyId(companyId);
           /* if (companyPositionList.size() == 0) {
                throw new Exception(companyInfo.getName() + "为设置机构层级");
            }*/

            List<String> positionList = new ArrayList<>();
            if (companyPositionList.size() > 0 && companyStructureInfoList.size()>0) {
                CompanyPosition companyPosition = companyPositionList.get(0);
                List<String> list = companyPositionService.findAllPosition(companyPosition);
                positionList = list;
            }

            Map<String, Object> map = new HashMap<>();
            map.put("structureNameList", positionList);
            map.put("structureInfoList", companyStructureInfoList);


            messageResult.setData(map);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }


    @PostMapping("companyStructureByParentId")
    @ApiOperation(value = "按照公司id查询结构列表（公共接口）(多层级机构使用)")
    public MessageResult<List> companyStructureByParentId(String id) {
        MessageResult<List> messageResult = new MessageResult<>();

        try {


            List<Map<String, Object>> listMap = new ArrayList<>();
            List<CompanyStructureInfo> companyStructureInfoList = companyStructureInfoService.findByParentId(id);

            messageResult.setData(companyStructureInfoList);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("companyStructureByName")
    @ApiOperation(value = "按照机构名称查询结构列表（公共接口）(多层级机构使用)")
    public MessageResult<List> companyStructureByName(String companyId, String positionName) {
        MessageResult<List> messageResult = new MessageResult<>();

        try {


            List<Map<String, Object>> listMap = new ArrayList<>();
            List<CompanyStructureInfo> companyStructureInfoList = companyStructureInfoService.findByCompanyIdAndPositionName(companyId, positionName);

            messageResult.setData(companyStructureInfoList);
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

    @PostMapping("companyBySceneAndName")
    @ApiOperation(value = "按照公司名称和场景查询（公共接口）")
    public MessageResult<Map> companyBySceneAndName(String sceneId, String name, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {


            Map<String, Object> searchParams = new HashMap<>();
            searchParams.put("name", "%" + name + "%");
            searchParams.put("sceneId", sceneId);
            searchParams.put("enableRegister",true);
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time", "desc"));
            Page<CompanyInfo> page = companyInfoService.pageSearch(searchParams, pageIndex, pageSize, sortList);
            messageResult.setData(PojoUtils.pageWrapper(page));
            messageResult.setResult(true);
            messageResult.setCode(200);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setResult(false);
            messageResult.setMessage(ex.getMessage());
        }

        return messageResult;
    }

}
