package com.xhpc.user.service.impl;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcRechargeOrder;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.dto.MechanismDto;
import com.xhpc.user.mapper.MechanismMapper;
import com.xhpc.user.service.IMechanismService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @date 2021/12/20 14:30
 */
@Service
public class IMechanismServiceImpl implements IMechanismService {

    @Autowired
    private MechanismMapper mechanismMapper;
    @Autowired
    private TokenService tokenService;

    @Override
    public List<MechanismDto> dataList(Integer status) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = sysUser.getUserId();
        int type =1;
        //根据权限获取场站
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(logUserId)){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                logUserId=logOperatorId;
            }else{
                type =2;
            }
        }else{
            type=0;
        }
        if(status==0){
            return regionTree(logUserId,type,tenantId);
        }else if(status==1){
            return operatorTree(logUserId,type,tenantId);
        }else if(status==2){
            return operatorRegionTree(logUserId,type,tenantId);
        }else{
            return tenantOperatorTree(logUserId,type);
        }
    }

    @Override
    public int updateMechanism(Long mechanismId, Integer source) {
        return mechanismMapper.updateMechanism(mechanismId, source);
    }

    @Override
    public int addMechanism(List<String> chargingStationIds, Long mechanismId, Integer source) {
        return mechanismMapper.insertMechanism(chargingStationIds, mechanismId, source);
    }

    @Override
    public Long addRechargeOrder(Long userId, Integer source, BigDecimal amount,BigDecimal remainingSum,Integer type) {
        String orderNumber = StringUtils.numFormat(userId, 1, StatusConstants.FLOWING_WATER_RECHARGE_TYPE);

        XhpcRechargeOrder xhpcRechargeOrder =new XhpcRechargeOrder();
        xhpcRechargeOrder.setUserId(userId);
        xhpcRechargeOrder.setRechargeOrderNumber(orderNumber);
        xhpcRechargeOrder.setAmount(amount);
        if(UserTypeUtil.RECHARGE_PT.equals(type)){
            xhpcRechargeOrder.setStatus(UserTypeUtil.RECHARGE_STATUS_ONE);
        }else{
            xhpcRechargeOrder.setStatus(UserTypeUtil.RECHARGE_STATUS_ZERO);
        }
        xhpcRechargeOrder.setType(type);
        xhpcRechargeOrder.setSource(source);
        xhpcRechargeOrder.setCreateTime(new Date());
        mechanismMapper.insertRechargeOrder(xhpcRechargeOrder);
        return xhpcRechargeOrder.getRechargeOrderId();
    }

    /**
     * 用户流水记录
     * @param amount              流水金额
     * @param remainingSum        余额
     * @param chargeOrderId      充电订单id
     * @param rechargeOrderId     充值订单id
     * @param refundOrderId       退款订单id
     * @param type                1充值 2退款 3充电
     * @param tenantId            租户id
     * @param source              订单来源（0 C端用户 1 流量方用户 2社区用户 3B端用户）
     * @return
     */
    @Override
    public int insertUserAccount(Long userId,BigDecimal amount,BigDecimal remainingSum,Long chargeOrderId,Long rechargeOrderId,Long refundOrderId,Integer type,String tenantId,Integer source) {

        return mechanismMapper.insertUserAccount(userId, amount, remainingSum, chargeOrderId, rechargeOrderId, refundOrderId, type, tenantId, source,new Date());
    }


    /**
     * 地区树
     * @param userId
     * @param type
     * @return
     */
    public List<MechanismDto> regionTree(Long userId, Integer type,String tenantId){
        List<MechanismDto> mechanismDtos =new ArrayList<>();
        //省
        List<Map<String, Object>> list = mechanismMapper.groupProvinceById(userId, type,null);
        if(list !=null && list.size()>0){
            for (Map<String, Object> map:list) {
                MechanismDto mechanismDto =new MechanismDto();
                String code = map.get("code").toString();
                mechanismDto.setId("Shen"+code);
                mechanismDto.setLabel(map.get("name").toString());
                List<MechanismDto> mechanismCityList =new ArrayList<>();
                //市
                List<Map<String, Object>> cityList = mechanismMapper.groupCityById(userId, type, code,null);
                for (Map<String, Object> cityMap:cityList) {
                    MechanismDto mechanismCity =new MechanismDto();
                    String codeCity = cityMap.get("code").toString();
                    mechanismCity.setId("Shi"+codeCity);
                    mechanismCity.setLabel(cityMap.get("name").toString());
                    List<MechanismDto> mechanismAreaList =new ArrayList<>();
                    //区
                    List<Map<String, Object>> areaList = mechanismMapper.groupAreaById(userId, type, codeCity,null);
                    for (Map<String, Object> areaMap:areaList) {
                        MechanismDto mechanismArea =new MechanismDto();
                        String codeArea = areaMap.get("code").toString();
                        mechanismArea.setId("Qu"+codeArea);
                        mechanismArea.setLabel(areaMap.get("name").toString());
                        List<MechanismDto> mechanismOperatorList =new ArrayList<>();
                        //运营商
                        List<Map<String, Object>> operatorList = mechanismMapper.groupOperatorById(userId, type, codeArea,tenantId);
                        for (Map<String, Object> operatorMap:operatorList) {
                            MechanismDto mechanismOperator =new MechanismDto();
                            String operatorId = operatorMap.get("operatorId").toString();
                            mechanismOperator.setId("Yun"+operatorId);
                            mechanismOperator.setLabel(operatorMap.get("name").toString());
                            List<MechanismDto> mechanismdataList =new ArrayList<>();
                            //场站
                            List<Map<String, Object>> dataList = mechanismMapper.dataChargingStationId(userId, type, operatorId,codeArea,tenantId);
                            for (Map<String, Object> dataMap:dataList) {
                                MechanismDto mechanismData =new MechanismDto();
                                String codeData = dataMap.get("chargingStationId").toString();
                                mechanismData.setId(codeData);
                                mechanismData.setLabel(dataMap.get("name").toString());
                                mechanismdataList.add(mechanismData);
                            }
                            mechanismOperator.setChildren(mechanismdataList);
                            mechanismOperatorList.add(mechanismOperator);
                        }
                        mechanismArea.setChildren(mechanismOperatorList);
                        mechanismAreaList.add(mechanismArea);
                    }
                    mechanismCity.setChildren(mechanismAreaList);
                    mechanismCityList.add(mechanismCity);
                }
                mechanismDto.setChildren(mechanismCityList);
                mechanismDtos.add(mechanismDto);
            }
        }
        return mechanismDtos;
    }

    /**
     * 运营商树
     */
    public List<MechanismDto> operatorTree(Long userId, Integer type,String tenantId){
        List<MechanismDto> mechanismDtos =new ArrayList<>();
        List<Map<String, Object>> operatorList = mechanismMapper.groupOperatorById(userId, type, null,tenantId);
        if(operatorList !=null && operatorList.size()>0){
            for (Map<String, Object> operatorMap:operatorList) {
                MechanismDto mechanismOperator =new MechanismDto();
                String operatorId = operatorMap.get("operatorId").toString();
                mechanismOperator.setId("Yun"+operatorId);
                mechanismOperator.setLabel(operatorMap.get("name").toString());
                List<MechanismDto> mechanismdataList =new ArrayList<>();
                //场站
                List<Map<String, Object>> dataList = mechanismMapper.dataChargingStationId(userId, type, operatorId,null,tenantId);
                for (Map<String, Object> dataMap:dataList) {
                    MechanismDto mechanismData =new MechanismDto();
                    String codeData = dataMap.get("chargingStationId").toString();
                    mechanismData.setId(codeData);
                    mechanismData.setLabel(dataMap.get("name").toString());
                    mechanismdataList.add(mechanismData);
                }
                mechanismOperator.setChildren(mechanismdataList);
                mechanismDtos.add(mechanismOperator);
            }
        }
        return mechanismDtos;
    }

    /**
     * 运营商--地区树
     * @param userId
     * @param type
     * @return
     */
    public List<MechanismDto> operatorRegionTree(Long userId, Integer type,String tenantId){

        List<MechanismDto> mechanismDtos =new ArrayList<>();
        List<Map<String, Object>> operatorList = mechanismMapper.groupOperatorById(userId, type, null,tenantId);
        if(operatorList !=null && operatorList.size()>0){
            for (Map<String, Object> operatorMap:operatorList) {
                MechanismDto mechanismOperator =new MechanismDto();
                String operatorId = operatorMap.get("operatorId").toString();
                mechanismOperator.setId("Yun"+operatorId);
                mechanismOperator.setLabel(operatorMap.get("name").toString());
                List<MechanismDto> mechanismdataList =new ArrayList<>();
                //省
                List<Map<String, Object>> list = mechanismMapper.groupProvinceById(userId, 3,Long.valueOf(operatorId));
                    for (Map<String, Object> map:list) {
                        MechanismDto mechanismDto =new MechanismDto();
                        String code = map.get("code").toString();
                        mechanismDto.setId("Shen"+code);
                        mechanismDto.setLabel(map.get("name").toString());
                        List<MechanismDto> mechanismCityList =new ArrayList<>();
                        //市
                        List<Map<String, Object>> cityList = mechanismMapper.groupCityById(userId, 3, code,Long.valueOf(operatorId));
                        for (Map<String, Object> cityMap:cityList) {
                            MechanismDto mechanismCity =new MechanismDto();
                            String codeCity = cityMap.get("code").toString();
                            mechanismCity.setId("Shi"+codeCity);
                            mechanismCity.setLabel(cityMap.get("name").toString());
                            List<MechanismDto> mechanismAreaList =new ArrayList<>();
                            //区
                            List<Map<String, Object>> areaList = mechanismMapper.groupAreaById(userId, 3,codeCity,Long.valueOf(operatorId));
                            for (Map<String, Object> areaMap:areaList) {
                                MechanismDto mechanismArea =new MechanismDto();
                                String codeArea = areaMap.get("code").toString();
                                mechanismArea.setId("Qu"+codeArea);
                                mechanismArea.setLabel(areaMap.get("name").toString());
                                List<MechanismDto> mechanismOperatorList =new ArrayList<>();
                                //场站
                                List<Map<String, Object>> dataList = mechanismMapper.dataChargingStationId(userId, type, operatorId,codeArea,tenantId);
                                for (Map<String, Object> dataMap:dataList) {
                                    MechanismDto mechanismData =new MechanismDto();
                                    String codeData = dataMap.get("chargingStationId").toString();
                                    mechanismData.setId(codeData);
                                    mechanismData.setLabel(dataMap.get("name").toString());
                                    mechanismOperatorList.add(mechanismData);
                                }
                                mechanismArea.setChildren(mechanismOperatorList);
                                mechanismAreaList.add(mechanismArea);
                            }
                            mechanismCity.setChildren(mechanismAreaList);
                            mechanismCityList.add(mechanismCity);
                        }
                        mechanismDto.setChildren(mechanismCityList);
                        mechanismdataList.add(mechanismDto);
                    }
                mechanismOperator.setChildren(mechanismdataList);
                mechanismDtos.add(mechanismOperator);
            }
        }
        return mechanismDtos;
    }

    /**
     * 租户-运营商-场站
     * @param userId
     * @param type
     * @return
     */
    public List<MechanismDto> tenantOperatorTree(Long userId, Integer type){
        List<MechanismDto> mechanismDtos =new ArrayList<>();
        //查询租户
        List<Map<String, Object>> tenantList = mechanismMapper.getTenantList(userId, type);
        if(tenantList !=null && tenantList.size()>0){
            for (Map<String, Object> tenantMap:tenantList) {
                MechanismDto mechanismTenant =new MechanismDto();
                String tenantId = tenantMap.get("tenantId").toString();
                mechanismTenant.setId("Tenant"+tenantId);
                mechanismTenant.setLabel(tenantMap.get("tenantName").toString());

                List<MechanismDto> mechanismOperatorList =new ArrayList<>();
                List<Map<String, Object>> operatorList = mechanismMapper.groupOperatorById(userId, type, null,tenantId);
                if(operatorList !=null && operatorList.size()>0){
                    for (Map<String, Object> operatorMap:operatorList) {
                        MechanismDto mechanismOperator =new MechanismDto();
                        String operatorId = operatorMap.get("operatorId").toString();
                        mechanismOperator.setId("Yun"+operatorId);
                        mechanismOperator.setLabel(operatorMap.get("name").toString());
                        List<MechanismDto> mechanismdataList =new ArrayList<>();
                        //场站
                        List<Map<String, Object>> dataList = mechanismMapper.dataChargingStationId(userId, type, operatorId,null,tenantId);
                        for (Map<String, Object> dataMap:dataList) {
                            MechanismDto mechanismData =new MechanismDto();
                            String codeData = dataMap.get("chargingStationId").toString();
                            mechanismData.setId(codeData);
                            mechanismData.setLabel(dataMap.get("name").toString());
                            mechanismdataList.add(mechanismData);
                        }
                        mechanismOperator.setChildren(mechanismdataList);
                        mechanismOperatorList.add(mechanismOperator);
                    }
                }
                mechanismTenant.setChildren(mechanismOperatorList);
                mechanismDtos.add(mechanismTenant);
            }
        }
        return mechanismDtos;




    }
}
