package com.ningdatech.carapi.sys.manage;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.common.constant.UserRoleTypeEnum;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.datascope.contants.LambdaDataScopeTypeEnum;
import com.ningdatech.carapi.industry.service.IOperatorIndexScoreInfoService;
import com.ningdatech.carapi.sys.entity.Operator;
import com.ningdatech.carapi.sys.entity.OperatorRegionHead;
import com.ningdatech.carapi.sys.entity.dto.OperatorPageQuery;
import com.ningdatech.carapi.sys.entity.dto.OperatorQueryDTO;
import com.ningdatech.carapi.sys.entity.vo.OperatorVO;
import com.ningdatech.carapi.sys.helper.OperatorsCacheHelper;
import com.ningdatech.carapi.sys.service.IOperatorScoreInfoService;
import com.ningdatech.carapi.sys.service.OperatorRegionHeadService;
import com.ningdatech.carapi.sys.service.OperatorService;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Classname OperatorManage
 * @Description
 * @Date 2022/10/12 11:26
 * @Created by PoffyZhang
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class OperatorManage {

    private final OperatorService operatorService;
    private final OperatorRegionHeadService operatorRegionHeadService;
    private final IOperatorScoreInfoService operatorScoreInfoService;

    private final IOperatorIndexScoreInfoService operatorIndexScoreInfoService;
    private final OperatorsCacheHelper operatorsCacheHelper;

    /**
     * 查询列表
     * @param operatorPageQuery
     * @return
     */
    public PageVo<OperatorVO> queryList(OperatorPageQuery operatorPageQuery) {
        // 按照登录用户，如果是运营商账号登录，删选出该运营商关联的车辆信息
        UserInfoDetails userDetail = LoginUserUtil.loginUserDetail();
        Long roleId = userDetail.getRoleId();
        if (UserRoleTypeEnum.OPERATOR_ROLE_ID.getId().equals(roleId)) {
            String companyName = userDetail.getCompanyName();
            // 暂时控制运营商名称和运营商所在企业名称相同
            OperatorQueryDTO operatorQueryDTO = operatorsCacheHelper.getByName(companyName);
            // 获取运营商id
            Long operatorId = operatorQueryDTO.getId();
            Page<Operator> page = operatorPageQuery.page();
            LambdaQueryWrapper<Operator> wrapper = Wrappers.lambdaQuery(Operator.class);
            wrapper.like(StringUtils.isNotBlank(operatorPageQuery.getName()), Operator::getName, operatorPageQuery.getName())
                    .eq(Operator::getId,operatorId)
                    .orderBy(Boolean.TRUE, Boolean.FALSE, Operator::getUpdateOn);
            operatorService.page(page, wrapper);
            List<OperatorVO> data = CollUtils.convert(page.getRecords(), c -> BeanUtil.copyProperties(c, OperatorVO.class));
            return PageVo.of(data, page.getTotal());
        }else {
            Page<Operator> page = operatorPageQuery.page();
            LambdaQueryWrapper<Operator> wrapper = Wrappers.lambdaQuery(Operator.class);
            wrapper.like(StringUtils.isNotBlank(operatorPageQuery.getName()), Operator::getName, operatorPageQuery.getName())
                    .orderBy(Boolean.TRUE, Boolean.FALSE, Operator::getUpdateOn);
            operatorService.page(page, wrapper);
            List<OperatorVO> data = CollUtils.convert(page.getRecords(), c -> BeanUtil.copyProperties(c, OperatorVO.class));
            return PageVo.of(data, page.getTotal());
        }
    }


    /**
     * 查询详情
     * @param id
     * @return
     */
    public OperatorQueryDTO getDetails(Long id) {
        Operator operator = operatorService.getById(id);
        OperatorQueryDTO query = BeanUtil.toBean(operator, OperatorQueryDTO.class);
        fillRegionHead(query);
        return query;
    }

    /**
     * 放入区域负责人
     * @param query
     */
    public void fillRegionHead(OperatorQueryDTO query) {
        if(Objects.nonNull(query)){
            LambdaQueryWrapper<OperatorRegionHead> wrapper = Wrappers.lambdaQuery(OperatorRegionHead.class);
            List<OperatorRegionHead> regionHeads = getOperatorRegionHeads(query, wrapper);
            CodeUtil.searchRegionNames(regionHeads);
            query.setOperatorRegionHeads(regionHeads);
        }
    }

    public List<OperatorRegionHead> getOperatorRegionHeads(OperatorQueryDTO query, LambdaQueryWrapper<OperatorRegionHead> wrapper) {
        OperatorManage context = (OperatorManage) AopContext.currentProxy();
        context.buildQuery(wrapper, query.getId());
        List<OperatorRegionHead> regionHeads = operatorRegionHeadService.list(wrapper);
        return regionHeads;
    }

    @LambdaDataScope(type = LambdaDataScopeTypeEnum.ONLY_REGION)
    public void buildQuery(LambdaQueryWrapper<OperatorRegionHead> wrapper, Long id) {
        wrapper.in(OperatorRegionHead::getOperatorId, id);
    }
}
