package cn.superdesk.uniorder.controller._common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.sys.SysOderUser;
import cn.superdesk.uniorder.dto.param._function.select.SelectOptionDepartmentCriteria;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionDepartment;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionOrg;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionVirt;
import cn.superdesk.uniorder.dubbo.consumer._permission.PermissionDubboCustomerService;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysOrganizationDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysOderUserDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2022/5/13 3:41 下午
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022/5/13 3:41 下午
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "公共接口-下拉列表", tags = {"公共接口-下拉列表"})
@Slf4j
@RestController
@RequestMapping("/common/select") // organisation
public class CommonSelectOrganisationController extends BaseController {

    @Autowired
    private SysOrganizationDubboConsumerService sysOrganizationDubboConsumerService;
    @Autowired
    private SysEnterpriseDubboConsumerService   sysEnterpriseDubboConsumerService;
    @Autowired
    private SysOderUserDubboConsumerService     sysOderUserDubboConsumerService;

    @Autowired
    private UnifiedOrderDubboConsumerService unifiedOrderDubboConsumerService;

    @Autowired
    private PermissionDubboCustomerService permissionDubboCustomerService;


    /****************************************** 组织架构 start ******************************************/

    /**
     * 市场管理-企业管理-项目下拉列表
     * /common/select/projectdata
     * TODO Mark 当前在用
     *
     * @return
     */
    @ApiOperation(value = "项目 select and search - 市场管理-企业管理-TODO Mark 当前在用")
    @RequestMapping(value = "/projectdata", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SelectOptionOrg>> getSelectOrg() {

        R<List<SelectOptionOrg>> result = new R<>();

        try {

            Map<String, Object> params = new HashMap<>();
            params.put("projectName", getString("projectName"));

            // 包含Token
            if (StrUtil.isNotBlank(getToken())) {

                List<SelectOptionOrg> data  = permissionDubboCustomerService.getSelectPermOrgByToken(getToken());
                int                   count = 0;

                // 配置了权限
                if (CollUtil.isNotEmpty(data)) {
                    count = data.size();
                    result.setData(data);

                }
                // 这个补刀是对的,没配置权限时走这
                else {
                    result = sysOrganizationDubboConsumerService.getProjectSelect(params, getPageNum(), getPageSize());
                }
            }
            // 不包含Token
            else {
                result = sysOrganizationDubboConsumerService.getProjectSelect(params, getPageNum(), getPageSize());
            }

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 项目列表-用户权限内的
     *
     * @return
     */
    @ApiOperation(value = "项目  select - 用户权限内的")
    @RequestMapping(value = "/getSelectPermOrg", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SelectOptionOrg>> getSelectPermOrg() {

        R<List<SelectOptionOrg>> result = new R<>();

        try {

            List<SelectOptionOrg> listSelectOptionOrg = permissionDubboCustomerService.getSelectPermOrgByToken(getToken());
            result.setData(listSelectOptionOrg);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 企业下拉
     * TODO Mark 当前在用
     * /common/select/virtdata
     *
     * @return
     */
    @ApiOperation(value = "企业 select and search")
    @RequestMapping(value = "/virtdata", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SelectOptionVirt>> getSelectVirt() {

        R<List<SelectOptionVirt>> response = new R<>();

        try {

            Map<String, Object> params = new HashMap<>();

            params.put("virtName", getString("virtName"));
            params.put("projectId", getLong("projectId"));
            params.put("orderByClause", getString("orderByClause"));

            /**
             * orderByClause
             * sync_time desc ---- 同步时间 倒序
             * id asc ---- 结算中心Id 升序
             * enterprise_id asc ---- 第三方企业Id 升序
             */

            // isNotInUnifiedCustomer 为 0 是要 联动
            if (getInteger("isNotInUnifiedCustomer") == null) {
                params.put("notuse", 1);
            } else {
                params.put("notuse", getInteger("isNotInUnifiedCustomer"));
            }

            // 包含Token
            if (StrUtil.isNotBlank(getToken())) {

                List<SelectOptionVirt> listSelectOptionVirt = permissionDubboCustomerService
                        .getSelectPermVirtByToken(
                                getToken()
                        );

                // 配置了数据权限
                if (CollUtil.isNotEmpty(listSelectOptionVirt)) {

                    List<SelectOptionVirt> listSelectOptionVirtFilter = new ArrayList<>();

                    if (CollUtil.isNotEmpty(listSelectOptionVirt)) {

                        // 联动
                        if (params.get("notuse").equals(0)
                                && null != params.get("projectId") //
                                && StrUtil.isNotBlank(params.get("projectId").toString())) {

                            listSelectOptionVirtFilter = listSelectOptionVirt
                                    .stream()
                                    .filter(o -> o.getOutOrgId().equals(params.get("projectId")))
                                    .collect(Collectors.toList());

                            response.setData(listSelectOptionVirtFilter);

                        } else {
                            response.setData(listSelectOptionVirt);
                        }
                    } else {
                        response.setData(listSelectOptionVirt);
                    }
                }
                // 配置了数据权限 但没找到
                else {
                    response = sysEnterpriseDubboConsumerService
                            .getVirtSelect(
                                    params,
                                    getPageNum(),
                                    getPageSize(),
                                    getToken()
                            );
                }

            }
            // 不包含Token
            else {
                response = sysEnterpriseDubboConsumerService
                        .getVirtSelect(
                                params,
                                getPageNum(),
                                getPageSize(),
                                getToken()
                        );
            }


        } catch (Exception e) {

            response.setCode(String.valueOf(e.hashCode()));
            response.setSuccess(false);
            response.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return response;
    }

    /**
     * 企业列表-用户权限内的
     *
     * @return
     */
    @ApiOperation(value = "企业 select - 用户权限内的")
    @RequestMapping(value = "/getSelectPermVirt", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SelectOptionVirt>> getSelectPermVirt() {

        R<List<SelectOptionVirt>> result = new R<>();

        try {

            List<SelectOptionVirt> listSelectOptionVirt = permissionDubboCustomerService.getSelectPermVirtByToken(getToken());
            result.setData(listSelectOptionVirt);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 企业下拉列表新
     * <p>
     * 报表管理-企业统计 Search 企业名称
     * 订单管理-订单管理 Search 企业名称
     *
     * @return
     */
    @ApiOperation(value = "企业 select and search \n" +
            "报表管理-企业统计 Search 企业名称 \n" +
            "订单管理-订单管理 Search 企业名称")
    @RequestMapping(value = "/virtalldata", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SelectOptionVirt>> getSelectVirtAll() {

        R<List<SelectOptionVirt>> result = new R<>();

        try {
            Map<String, Object> params = new HashMap<>();
            params.put("virtName", getString("virtName"));
            params.put("projectId", getLong("projectId"));

            result = sysEnterpriseDubboConsumerService.getVirtSelect(params, getPageNum(), getPageSize(), getToken());

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 企业人员列表
     *
     * @return
     */
    @ApiOperation(value = "企业人员列表")
    @RequestMapping(value = "/virtuserdata", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SysOderUser>> getVirtUserData() {

        R<List<SysOderUser>> result = new R<>();

        try {
            Map<String, Object> params = new HashMap<>();

            params.put("virtId", getLong("virtId"));
            params.put("projectId", getLong("projectId"));
            params.put("mobile", getString("mobile"));
            params.put("fullname", getString("fullname"));

            List<SysOderUser> orderUsers = sysOderUserDubboConsumerService.findAll(params);

            result.setSuccess(true);
            result.setData(orderUsers);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 部门下拉列表
     * /common/select/getSelectPurchaseDepartment
     *
     * @return
     */
    @ApiOperation(value = "部门下拉列表-TODO Mark 当前在用")
    @RequestMapping(value = "/getSelectPurchaseDepartment", method = RequestMethod.POST)
    @ResponseBody
    public R<List<SelectOptionDepartment>> getSelectPurchaseDepartment(
            @RequestBody SelectOptionDepartmentCriteria criteria) {

        R<List<SelectOptionDepartment>> result = new R<>();

        try {

            List<SelectOptionDepartment> listSelectOptionVirt = unifiedOrderDubboConsumerService
                    .getSelectPurchaseDepartment(
                            criteria,
                            getToken()
                    );
            result.setData(listSelectOptionVirt);

        } catch (Exception e) {

            result.setCode(String.valueOf(e.hashCode()));
            result.setSuccess(false);
            result.setMessage(e.getMessage());

            log.error(ExceptionUtils.getMessage(e));
            e.printStackTrace();
        }

        return result;
    }

    /****************************************** 组织架构   end ******************************************/


    /****************************************** 开票发票 start ******************************************/


    /****************************************** 开票发票 start ******************************************/


}
