package com.xhpc.user.service.impl;

import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.user.domain.XhpcBlacklist;
import com.xhpc.user.mapper.XhpcBlacklistMapper;
import com.xhpc.user.service.IMechanismService;
import com.xhpc.user.service.IXhpcBlacklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class XhpcBlacklistServiceImpl extends BaseService implements IXhpcBlacklistService {

    @Resource
    private XhpcBlacklistMapper xhpcBlacklistMapper;
    @Resource
    private TokenService tokenService;
    @Autowired
    private IMechanismService mechanismService;
    @Override
    public List<Map<String, Object>> getList(Map<String, Object> params) {
        startPage();
        return xhpcBlacklistMapper.getList(params);
    }

    @Override
    public AjaxResult addBlacklist(HttpServletRequest request, XhpcBlacklist xhpcBlacklist) {
        R r = judgeXhpcCustomers(xhpcBlacklist);
        if(r.getCode()!=200){
            return AjaxResult.error(r.getMsg());
        }
        //名称重复不能入库
        Integer distinguish = xhpcBlacklist.getDistinguish();
        if(distinguish==1){
            int phoneOrVin = xhpcBlacklistMapper.getPhoneOrVin(xhpcBlacklist.getPhone(), xhpcBlacklist.getPlateNum(),xhpcBlacklist.getBlacklistId(),distinguish);
            if(phoneOrVin >0 ){
                return AjaxResult.error("手机号或者车牌号重复");
            }
        }else{
//            int i = xhpcBlacklistMapper.getManufacturersOrVehicletype(xhpcBlacklist.getManufacturers(),null,null,null,xhpcBlacklist.getBlacklistId(),distinguish);
//            if(i >0 ){
//                return AjaxResult.error("制造厂商重复");
//            }
//            int i1 = xhpcBlacklistMapper.getManufacturersOrVehicletype(null,xhpcBlacklist.getVehicleType(),null,null,xhpcBlacklist.getBlacklistId(),distinguish);
//            if(i1 >0 ){
//                return AjaxResult.error("车辆类型重复");
//            }
//            int i2 = xhpcBlacklistMapper.getManufacturersOrVehicletype(null,null,xhpcBlacklist.getVehicleEngine(),null,xhpcBlacklist.getBlacklistId(),distinguish);
//            if(i2 >0 ){
//                return AjaxResult.error("发动机型式重复");
//            }
//            int i3 = xhpcBlacklistMapper.getManufacturersOrVehicletype(null,null,null,xhpcBlacklist.getVehicleCode(),xhpcBlacklist.getBlacklistId(),distinguish);
//            if(i3 >0 ){
//                return AjaxResult.error("企业车型代码重复");
//            }
        }


        String chargingStationIds = xhpcBlacklist.getChargingStationIds();
        if("".equals(chargingStationIds) || chargingStationIds ==null){
            xhpcBlacklist.setStatus(0);
        }else{
            xhpcBlacklist.setStatus(1);
        }

        if(xhpcBlacklist.getBlacklistId() ==null){
            xhpcBlacklistMapper.insertBlacklist(xhpcBlacklist);
            if(!"".equals(chargingStationIds) && chargingStationIds !=null){
                mechanismService.addMechanism(Arrays.asList(chargingStationIds.split(",")),xhpcBlacklist.getBlacklistId(),2);
            }
        }else{
            xhpcBlacklistMapper.updateBlacklist(xhpcBlacklist);
            mechanismService.updateMechanism(xhpcBlacklist.getBlacklistId(),2);
            if(!"".equals(chargingStationIds) && chargingStationIds !=null){
                mechanismService.addMechanism(Arrays.asList(chargingStationIds.split(",")),xhpcBlacklist.getBlacklistId(),2);
            }
        }


        return AjaxResult.success();
    }

    @Override
    public Map<String, Object> getBlacklistById(Long blacklistId) {
        return xhpcBlacklistMapper.getBlacklistById(blacklistId);
    }

    @Override
    public void deleteBlacklistById(Long blacklistId) {
        xhpcBlacklistMapper.deleteBlacklistById(blacklistId);
    }

    public R judgeXhpcCustomers(XhpcBlacklist xhpcBlacklist){
        if(xhpcBlacklist.getDistinguish()==1){
            if(xhpcBlacklist.getPhone()==null || "".equals(xhpcBlacklist.getPhone())){
                return R.fail("电话必填");
            }
            if(xhpcBlacklist.getPlateNum()==null || "".equals(xhpcBlacklist.getPlateNum())){
                return R.fail("车牌必填");
            }
        }

        if(xhpcBlacklist.getStartTime().getTime()>xhpcBlacklist.getEndTime().getTime()){
            return R.fail("限制充电开始时间大于结束时间");
        }


        return R.ok();
    }
}
