package com.xhpc.user.service.impl;

import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.service.BaseService;
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.domain.XhpcUserVehicle;
import com.xhpc.user.mapper.XhpcUserVehicleMapper;
import com.xhpc.user.service.IXhpcUserVehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author yuyang
 * @date 2023-10-19 17:06
 */
@Service
public class XhpcUserVehicleServiceImpl extends BaseService implements IXhpcUserVehicleService {

    @Resource
    XhpcUserVehicleMapper xhpcUserVehicleMapper;
    @Resource
    TokenService tokenService;

    @Override
    public R addUserVehicle(XhpcUserVehicle xhpcUserVehicle) {

        //查询VIN是否在黑名单
        if(xhpcUserVehicle.getVinSpecCode() !=null || !"".equals(xhpcUserVehicle.getVinSpecCode())){
            int vinBlacklist = xhpcUserVehicleMapper.getVinBlacklist(xhpcUserVehicle.getVinSpecCode());
            if(vinBlacklist>0){
                return R.fail("请联系客服添加VIN码");
            }
        }
        //标记该用户其他车牌号为空
        if(xhpcUserVehicle.getVehicleId() !=null){
            xhpcUserVehicle.setDelFlag(0);
            xhpcUserVehicleMapper.updateUserVehicle(xhpcUserVehicle);
        }else{
            //查询是否存在相同的车牌号,车牌相同以前的删除
            xhpcUserVehicleMapper.updateVehicleNameUserId(xhpcUserVehicle.getAppUserId(),xhpcUserVehicle.getVehicleName());
            int i = xhpcUserVehicleMapper.xhpcUserVehicleInformation(xhpcUserVehicle.getAppUserId(), xhpcUserVehicle.getSource(), 0);
            if(i==0){
                xhpcUserVehicle.setType(1);
            }
            xhpcUserVehicleMapper.addUserVehicle(xhpcUserVehicle);
        }
        return R.ok();
    }

    @Override
    public List<Map<String, Object>> list(HttpServletRequest request, String vehicleName,String phone,String chargingStationName) {

        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        String tenantId = loginUser.getTenantId();
        //桩的统计、该时段金额
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcUserVehicleMapper.list(vehicleName,phone,chargingStationName,logOperatorId,1,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcUserVehicleMapper.list(vehicleName,phone,chargingStationName,sysUserId,2,tenantId);
            }
        }else{
            startPage();
            list = xhpcUserVehicleMapper.list(vehicleName,phone,chargingStationName,sysUserId,0,tenantId);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getUserVehicleList(HttpServletRequest request,Long userId,Integer source) {
        //获取登陆用户
//        LoginUser loginUser = tokenService.getLoginUser(request);
//        String tenantId = loginUser.getTenantId();
        List<Map<String, Object>> list = xhpcUserVehicleMapper.getUserVehicleList(userId,source,"");
        return list;
    }

    @Override
    public R getXhpcUserVehicleMessage(Long userId, Integer source) {
        Map<String, Object> map = xhpcUserVehicleMapper.getXhpcUserVehicleMessage(userId, source, 1);
        if(map == null){
            return R.ok(xhpcUserVehicleMapper.getXhpcUserVehicleMessage(userId, source, null));
        }
        return R.ok(map);
    }

    @Override
    public R deleteUserVehicleById(Long vehicleId) {
        xhpcUserVehicleMapper.deleteUserVehicleById(vehicleId);
        return R.ok();
    }

    @Override
    public R defaultUserVehicle(Long vehicleId, Long appUserId, Integer source) {

        xhpcUserVehicleMapper.defaultUserVehicleList(vehicleId,appUserId,source);
        xhpcUserVehicleMapper.defaultUserVehicle(vehicleId);
        return R.ok();
    }

    @Override
    public R enlightenedUserVehicle(Long vehicleId, Long appUserId, Integer source,Integer stauts) {
        xhpcUserVehicleMapper.enlightenedUserVehicle(vehicleId,stauts);
        return R.ok();
    }
}
