package cn.com.ty.lift.enterprise.organization.service;

import cn.com.ty.lift.enterprise.organization.dao.entity.Organization;
import cn.com.ty.lift.enterprise.organization.dao.entity.Project;
import cn.com.ty.lift.enterprise.organization.service.impl.ProjectServiceImpl;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class OrganizationService {
    private final ProjectServiceImpl projectService;

    /**
     * 查询项目相关人员
     *
     * @param project 项目id
     * @return RestResponse 项目相关人员
     */
    public RestResponse<?> users(Project project) {
        List<HashMap<String, Object>> projectUsers = projectService.getBaseMapper().users(project.getId());
        if (projectUsers.isEmpty()) {
            return RestResponse.success();
        }

        projectUsers.forEach(projectUser -> {
            Object role = projectUser.get("role");
            if (role != null) {
                switch (role.toString()) {
                    case "ROLEMTWORK":
                        projectUser.replace("role", "维保工");
                        break;
                    case "CLERK":
                        projectUser.replace("role", "文员");
                        break;
                    case "REGION_DIRECTOR":
                        projectUser.replace("role", "区域主管");
                        break;
                    case "HIGH_DIRECTOR":
                        projectUser.replace("role", "高级主管");
                        break;
                }
            }
            Object monitor = projectUser.get("monitor");
            if (null != monitor) {
                if (monitor.equals("1")) {
                    projectUser.replace("role", "维保班长");
                }
            }
        });
        return RestResponse.success(projectUsers);
    }

    /**
     * 获取省，市，区，项目列表
     *
     * @return 省，市，区，项目列表
     * @since 2019/12/17 10:05
     */
    public RestResponse<?> organizationTree(Long mtCompanyId) {
        List<Organization> ps = new ArrayList<>();
        List<Organization> cs = new ArrayList<>();
        List<Organization> ds = new ArrayList<>();

        Organization organization;
        //项目列表
        List<Project> projects = projectService.list(new QueryWrapper<Project>()
                .select("id", "project_name", "province", "city", "district", "num")
                .isNotNull("province")
                .isNotNull("city")
                .isNotNull("district")
                .isNotNull("num")
                .isNotNull("region_id")
                .eq("mt_company_id", mtCompanyId)
        );

        //遍历项目
        for (Project project : projects) {
            if (project.getProjectName() == null || project.getDistrict() == null || project.getCity() == null || project.getProvince() == null)
                continue;
            organization = new Organization();
            //区名
            String district = project.getDistrict();
            //市名
            String city = project.getCity();
            //省名
            String province = project.getProvince();

            organization.setAreaName(district);
            organization.setLevel(3);
            //获取区列表和区下的项目
            if (ds.isEmpty()) {
                organization.setProjects(new ArrayList<>());
                organization.getProjects().add(project);
                ds.add(organization);
            } else {
                //
                boolean flag = false;
                for (Organization d : ds) {
                    if (d.getAreaName().equals(district)) {
                        d.getProjects().add(project);
                        flag = false;
                        break;
                    } else {
                        flag = true;
                    }
                }
                if (flag) {
                    organization.setProjects(new ArrayList<>());
                    organization.getProjects().add(project);
                    ds.add(organization);
                }
            }
            organization = new Organization();

            organization.setAreaName(city);
            organization.setLevel(2);
            //获取市列表
            if (cs.isEmpty()) {
                cs.add(organization);
            } else {
                boolean flag = true;
                for (Organization c : cs) {
                    if (c.getAreaName().equals(city)) {
                        flag = false;
                    }
                }
                if (flag)
                    cs.add(organization);
            }
            organization = new Organization();

            organization.setAreaName(province);
            organization.setLevel(1);
            //获取省列表
            if (ps.isEmpty()) {
                ps.add(organization);
            } else {
                boolean flag = true;
                for (Organization p : ps) {
                    if (p.getAreaName().equals(province)) {
                        flag = false;
                    }
                }
                if (flag)
                    ps.add(organization);
            }
        }

        for (Organization d : ds) {
            //获取市名
            String city = d.getProjects().get(0).getCity();
            for (Organization c : cs) {
                if (c.getAreaName().equals(city)) {
                    if (c.getOrganizations() == null) {
                        c.setOrganizations(new ArrayList<>());
                    }
                    c.getOrganizations().add(d);
                }
            }
        }

        for (Organization c : cs) {
            //防止空指针 ---
            List<Organization> organizations = c.getOrganizations();
            if (organizations == null || organizations.get(0) == null) {
                continue;
            }
            List<Project> projectsList = organizations.get(0).getProjects();
            if (projectsList == null || projectsList.get(0) == null) {
                continue;
            }
            //防止空指针 ---

            //获取省名
            String province = c.getOrganizations().get(0).getProjects().get(0).getProvince();
            for (Organization p : ps) {
                if (p.getAreaName().equals(province)) {
                    if (p.getOrganizations() == null) {
                        p.setOrganizations(new ArrayList<>());
                    }
                    p.getOrganizations().add(c);
                }
            }
        }
        if (ps.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(ps);
    }
}