package com.dcsoft.system.vehicle.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.SysPoint;
import com.dcsoft.system.domain.SysSpace;
import com.dcsoft.system.domain.vo.SysAuthTree;
import com.dcsoft.system.kq.domain.KqAdjust;
import com.dcsoft.system.mapper.SysPeopleMapper;
import com.dcsoft.system.vehicle.domain.CarPark;
import com.dcsoft.system.vehicle.domain.CarParkItem;
import com.dcsoft.system.vehicle.domain.CarParkRecord;
import com.dcsoft.system.vehicle.mapper.CarInfoMapper;
import com.dcsoft.system.vehicle.mapper.CarParkItemMapper;
import com.dcsoft.system.vehicle.mapper.CarParkMapper;
import com.dcsoft.system.vehicle.mapper.CarParkRecordMapper;
import com.dcsoft.system.vehicle.service.ICarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcsoft.system.vehicle.domain.CarInfo;

/**
 * 车辆信息Service业务层处理
 *
 * @author nichun
 * @date 2023-12-26
 */
@Service
public class CarInfoServiceImpl implements ICarInfoService
{
    @Autowired
    private CarInfoMapper carInfoMapper;

    @Autowired
    private CarParkMapper carParkMapper;


    @Autowired
    private CarParkItemMapper carParkItemMapper;

    @Autowired
    private CarParkRecordMapper carParkRecordMapper;

    @Autowired
    private SysPeopleMapper peopleMapper;

    /**
     * 查询车辆信息
     *
     * @param customerId 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public CarInfo selectCarInfoByCustomerId(Long customerId)
    {
        return carInfoMapper.selectCarInfoByCustomerId(customerId);
    }

    /**
     * 查询车辆信息列表
     *
     * @param carInfo 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<CarInfo> selectCarInfoList(CarInfo carInfo)
    {
        return carInfoMapper.selectCarInfoList(carInfo);
    }

    /**
     * 新增车辆信息
     *
     * @param carInfo 车辆信息
     * @return 结果
     */
    @Override
    public int insertCarInfo(CarInfo carInfo)
    {
        carInfo.setCreateTime(DateUtils.getNowDate());
        return carInfoMapper.insertCarInfo(carInfo);
    }

    /**
     * 修改车辆信息
     *
     * @param carInfo 车辆信息
     * @return 结果
     */
    @Override
    public int updateCarInfo(CarInfo carInfo)
    {
        carInfo.setUpdateTime(DateUtils.getNowDate());
        return carInfoMapper.updateCarInfo(carInfo);
    }

    /**
     * 批量删除车辆信息
     *
     * @param customerIds 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteCarInfoByCustomerIds(Long[] customerIds)
    {
        return carInfoMapper.deleteCarInfoByCustomerIds(customerIds);
    }

    /**
     * 删除车辆信息信息
     *
     * @param customerId 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteCarInfoByCustomerId(Long customerId)
    {
        return carInfoMapper.deleteCarInfoByCustomerId(customerId);
    }

    @Override
    public int addOrUpdate(SysPeople sysPeople) {
        CarInfo carInfo=carInfoMapper.selectCarInfoByCarNo(sysPeople.getCarNo());
        if(carInfo==null){
            carInfo=new CarInfo();
            carInfo.setPeopleId(sysPeople.getId());
            List<CarPark>  list=carParkMapper.selectCarParkList(new CarPark());
            CarPark park=list.get(0);
            carInfo.setParkId(park.getId());
            carInfo.setPlate(sysPeople.getCarNo());
            carInfo.setEnableTime(new Date());
            Calendar calendar = new GregorianCalendar();
            Date date = new Date();
            calendar.setTime(date);
            calendar.add(calendar.YEAR, 1);//把日期往后增加一年.整数往后推,负数往前移动
            date=calendar.getTime();   //这个时间就是日期往后推一天的结果
            carInfo.setOverdueTime(date);
            carInfo.setEnable("1");
            carInfo.setTimeSegEnable("0");
            carInfo.setNeedAlarm("0");
            //根据车场查询车辆
            carInfoMapper.insertCarInfo(carInfo);
            CarParkItem carParkItem=new CarParkItem();
            carParkItem.setParkId(carInfo.getParkId());
            List<CarParkItem> itemList=carParkItemMapper.selectCarParkItemList(carParkItem);
            for(CarParkItem a:itemList){
                CarParkRecord carParkRecord=new CarParkRecord();
                carParkRecord.setCustomerId(carInfo.getCustomerId());
                carParkRecord.setParkId(carInfo.getParkId());
                carParkRecord.setEquipmentId(a.getEquipmentId());
                carParkRecordMapper.insertCarParkRecord(carParkRecord);
            }
        }/*else{
            carInfo.setPeopleId(sysPeople.getId());
            carInfo.setPlate(sysPeople.getCarNo());
            //根据车场查询车辆
            if(carInfo.getParkId()!=null){
                CarParkItem carParkItem=new CarParkItem();
                carParkItem.setParkId(carInfo.getParkId());
                List<CarParkItem> itemList=carParkItemMapper.selectCarParkItemList(carParkItem);
                for(CarParkItem a:itemList){
                    CarParkRecord carParkRecord=new CarParkRecord();
                    carParkRecord.setCustomerId(carInfo.getCustomerId());
                    carParkRecord.setParkId(carInfo.getParkId());
                    carParkRecord.setEquipmentId(a.getEquipmentId());
                    carParkRecordMapper.insertCarParkRecord(carParkRecord);
                }
            }
            carInfoMapper.updateCarInfo(carInfo);
        }*/
        return 0;
    }

    @Override
    public List<SysAuthTree> selectTree() {
        List<SysAuthTree> tree=new ArrayList<SysAuthTree>();
        List<CarPark> spaces  =carParkMapper.selectCarParkList(new CarPark());
        for(CarPark s:spaces){
            List<SysAuthTree> children=getChildren(s.getId());
            SysAuthTree a=new SysAuthTree();
            a.setId(s.getId());
            a.setLabel(s.getName());
            a.setType("0");
            a.setChildren(children);
            tree.add(a);
        }

        return tree;
    }

    @Override
    public List<Map<String, Object>> carPlay(Long id) {
        return carParkMapper.carPlay(id);
    }

    @Override
    public List<Map<String, Object>> carPlayRecord(Long id) {
        return carParkMapper.carPlayRecord(id);
    }

    @Override
    public int selectCarInfo(Long id,String flag) {
        carParkMapper.deleteSelectCarInfo(id);
        return carParkMapper.selectCarInfo(id,flag);
    }

    private List<SysAuthTree> getChildren(Long id) {
        List<SysAuthTree> tree=new ArrayList<>();
        List<Map<String,Object>> points  =carParkMapper.selectItemTreeList(id);
        for(Map<String,Object> p:points){
            SysAuthTree a=new SysAuthTree();
            a.setId(Long.parseLong(p.get("id")+""));
            a.setLabel(p.get("name")+"");
            a.setType("1");
            a.setParentId(id);
            tree.add(a);
        }
        return tree;
    }

    /**
     * 导入用户数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importData(List<CarInfo> userList, boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i=0;
        for (CarInfo user : userList)
        {
            i++;
            try
            {
                // 验证是否存在这个用户
                CarInfo u = carInfoMapper.selectCarInfoByCarNo(user.getPlate());

                SysPeople people=peopleMapper.selectSysPeopleByUserName(user.getPeopleName());
                if(people!=null){
                    user.setPeopleId(people.getId());
                }

                CarPark carPark=carParkMapper.selectCarParkByName(user.getParkName());
                if(carPark!=null){
                    user.setParkId(carPark.getId());
                }
                if (StringUtils.isNull(u))
                {
                    user.setCarType("0");
                    user.setCreateBy(operName);
                    this.insertCarInfo(user);
                    //保存车辆授权
                    //根据车场查询车辆
                    if(user.getParkId()!=null){
                        CarParkItem carParkItem=new CarParkItem();
                        carParkItem.setParkId(user.getParkId());
                        List<CarParkItem> itemList=carParkItemMapper.selectCarParkItemList(carParkItem);
                        for(CarParkItem a:itemList){
                            CarParkRecord carParkRecord=new CarParkRecord();
                            carParkRecord.setCustomerId(user.getCustomerId());
                            carParkRecord.setParkId(user.getParkId());
                            carParkRecord.setEquipmentId(a.getEquipmentId());
                            carParkRecordMapper.insertCarParkRecord(carParkRecord);
                        }
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、车牌信息 " + user.getPlate() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    user.setCarType("0");
                    user.setCustomerId(u.getCustomerId());
                    user.setUpdateBy(operName);
                    this.updateCarInfo(user);
                    //根据车场查询车辆
                    if(user.getParkId()!=null){
                        //删除车辆授权
                        carParkRecordMapper.deleteCarParkRecordByCustomerId(user.getCustomerId());
                        CarParkItem carParkItem=new CarParkItem();
                        carParkItem.setParkId(user.getParkId());
                        List<CarParkItem> itemList=carParkItemMapper.selectCarParkItemList(carParkItem);
                        for(CarParkItem a:itemList){
                            CarParkRecord carParkRecord=new CarParkRecord();
                            carParkRecord.setCustomerId(user.getCustomerId());
                            carParkRecord.setParkId(user.getParkId());
                            carParkRecord.setEquipmentId(a.getEquipmentId());
                            carParkRecordMapper.insertCarParkRecord(carParkRecord);
                        }
                    }
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、车牌信息 " + user.getPlate() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、车牌信息 " + user.getPlate() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、车牌信息 " + user.getPlate() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



}
