package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.rds.mybatis.PageHelper;
import com.cusc.nirvana.user.rnr.fp.constants.CryptKeyHelper;
import com.cusc.nirvana.user.rnr.fp.converter.FpCarInfoConverter;
import com.cusc.nirvana.user.rnr.fp.dao.FpCarInfoDao;
import com.cusc.nirvana.user.rnr.fp.dao.entity.FpCarInfoPO;
import com.cusc.nirvana.user.rnr.fp.dto.CarInfoBindingDTO;
import com.cusc.nirvana.user.rnr.fp.dto.FpCarInfoDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFpCarInfoService;
import com.cusc.nirvana.user.rnr.mg.constants.CommonDeleteEnum;
import com.cusc.nirvana.user.util.CuscStringUtils;
import com.cusc.nirvana.user.util.crypt.DataCryptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 车辆信息表(FpCarInfo)表服务实现类
 *
 * @author yuy336
 * @since 2022-06-28 19:46:25
 */
@Service
@Slf4j
public class FpCarInfoServiceImpl extends ServiceImpl<FpCarInfoDao, FpCarInfoPO> implements IFpCarInfoService {

    @Resource
    private  FpCarInfoDao fpCarInfoDao;

    @Autowired
    private DataCryptService dataCryptService ;

    /**
     * 通过UUID查询单条数据
     *
     * @param bean
     * @return 实例对象
     */
    @Override
    public FpCarInfoDTO getByUuid(FpCarInfoDTO bean) {
        FpCarInfoPO record = this.getPoByUuid(bean.getUuid());;
        return FpCarInfoConverter.INSTANCE.poToDto(record);
    }
    
     /**
     * 通过查询条件查询集合数据
     *
     * @param bean
     * @return 集合对象
     */
    @Override
    public List<FpCarInfoDTO> queryByList(FpCarInfoDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("vin",bean.getVin());
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        queryWrapper.orderByDesc("create_time");
        List<FpCarInfoPO> record = this.list(queryWrapper);
        return FpCarInfoConverter.INSTANCE.poListToDtoList(record);
    }

    /**
     * 分页查询
     *
     * @param bean 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<FpCarInfoDTO> queryByPage(FpCarInfoDTO bean) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(!StringUtils.isEmpty(bean.getOrgid()),"orgId",bean.getOrgid());
        queryWrapper.eq(!StringUtils.isEmpty(bean.getVin()),"vin", CryptKeyHelper.encrypt(bean.getVin()));
        queryWrapper.eq(!StringUtils.isEmpty(bean.getOrgid()),"orgId",bean.getOrgid());
        queryWrapper.le(!StringUtils.isEmpty(bean.getEndTime()),"create_time",bean.getEndTime());
        queryWrapper.ge(!StringUtils.isEmpty(bean.getStartTime()),"create_time",bean.getStartTime());
        queryWrapper.orderByDesc("create_time");
       Page<FpCarInfoPO> page =
                this.page(new Page<>(bean.getCurrPage(), bean.getPageSize()), queryWrapper);
        return PageHelper.convert(page, FpCarInfoDTO.class);
    }

    /**
     * 新增数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpCarInfoDTO insert(FpCarInfoDTO bean) {
       FpCarInfoPO fpCarInfoPO = FpCarInfoConverter.INSTANCE.dtoToPo(bean);
	   fpCarInfoPO.setUuid(CuscStringUtils.generateUuid());
        this.save(fpCarInfoPO);
		bean.setUuid(fpCarInfoPO.getUuid());
        return bean;
    }

     /**
       * @author: jk
       * @description: 批量插入
       * @date: 2022/6/29 14:05
       * @version: 1.0
       */
    @Override
    @Transactional
    public Response addBatch(CarInfoBindingDTO bean) {
        boolean flag = true;
        if(!CollectionUtils.isEmpty(bean.getBindingList())) {
            try {
                log.warn("addBatch入参{}", JSONObject.toJSONString(bean));
                List<FpCarInfoPO> list = this.dtoListToPoList(bean.getBindingList());
                int tota = fpCarInfoDao.batchSaveOrUpdate(list);
                if(tota>0){
                    flag =true;
                }else {
                    flag =false;
                }
            } catch (Exception e) {
                log.error("车辆信息入库失败",e);
                flag = false;
            }
        }
       return  flag ? Response.createSuccess():Response.createError();
    }

    public List<FpCarInfoPO> dtoListToPoList(List<FpCarInfoDTO> dtos) {
        if ( dtos == null ) {
            return null;
        }

        List<FpCarInfoPO> list = new ArrayList<FpCarInfoPO>( dtos.size() );
        for ( FpCarInfoDTO fpCarInfoDTO : dtos ) {
            list.add( dtoToPo( fpCarInfoDTO ) );
        }

        return list;
    }

    public FpCarInfoPO dtoToPo(FpCarInfoDTO bean) {
        if ( bean == null ) {
            return null;
        }
        FpCarInfoPO fpCarInfoPO = new FpCarInfoPO();

        fpCarInfoPO.setId( bean.getId() );
        fpCarInfoPO.setIsDelete( bean.getIsDelete() );
        fpCarInfoPO.setCreateTime( bean.getCreateTime() );
        fpCarInfoPO.setUpdateTime( bean.getUpdateTime() );
        fpCarInfoPO.setCreator( bean.getCreator() );
        fpCarInfoPO.setUuid( bean.getUuid() );
        fpCarInfoPO.setNameVehicleEnterprise( bean.getNameVehicleEnterprise() );
        fpCarInfoPO.setVin( dataCryptService.encryptData(bean.getVin()) );
        fpCarInfoPO.setVehicleType( bean.getVehicleType() );
        fpCarInfoPO.setPlaceOfOriginOfVehicle( bean.getPlaceOfOriginOfVehicle() );
        fpCarInfoPO.setVehicleDepartment( bean.getVehicleDepartment() );
        fpCarInfoPO.setVehicleName( bean.getVehicleName() );
        fpCarInfoPO.setVehicleNum( bean.getVehicleNum() );
        fpCarInfoPO.setVehicleModel( bean.getVehicleModel() );
        fpCarInfoPO.setBodyColor( bean.getBodyColor() );
        fpCarInfoPO.setFuelType( bean.getFuelType() );
        fpCarInfoPO.setEngineNum( bean.getEngineNum() );
        fpCarInfoPO.setMotorNum( bean.getMotorNum() );
        fpCarInfoPO.setVehicleDepartureTime( bean.getVehicleDepartureTime() );
        fpCarInfoPO.setVehicleSalesTime( bean.getVehicleSalesTime() );
        fpCarInfoPO.setVehicleSalesUpdateTime( bean.getVehicleSalesUpdateTime() );
        fpCarInfoPO.setLicensePlateNumber( dataCryptService.encryptData(bean.getLicensePlateNumber()) );
        fpCarInfoPO.setVehicleChannelName( bean.getVehicleChannelName() );
        fpCarInfoPO.setVehicleChannelType( bean.getVehicleChannelType() );
        fpCarInfoPO.setVehicleStaffName( dataCryptService.encryptData(bean.getVehicleStaffName()) );
        fpCarInfoPO.setVehicleLogginAddress( bean.getVehicleLogginAddress() );
        fpCarInfoPO.setOrgid( bean.getOrgid() );
        fpCarInfoPO.setTenantno( bean.getTenantno() );
        fpCarInfoPO.setOperator( bean.getOperator() );
        fpCarInfoPO.setTagId( bean.getTagId() );

        return fpCarInfoPO;
    }
    /**
     * 修改数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpCarInfoDTO update(FpCarInfoDTO bean) {
        FpCarInfoPO fpCarInfoPO = this.getPoByUuid(bean.getUuid());
		 if(fpCarInfoPO == null){
            return null;
        }
		FpCarInfoPO tmpBean = FpCarInfoConverter.INSTANCE.dtoToPo(bean);
		tmpBean.setId(fpCarInfoPO.getId());
        this.updateById(tmpBean);
        return bean;
    }

    /**
     * 通过主键删除数据
     * @param bean 实例对象
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(FpCarInfoDTO bean) {
        FpCarInfoPO fpCarInfoPO = this.getPoByUuid(bean.getUuid());
		 if (fpCarInfoPO == null) {
            return false;
        }
        FpCarInfoPO tmpBean = new FpCarInfoPO();
        tmpBean.setId(fpCarInfoPO.getId());
        tmpBean.setIsDelete(CommonDeleteEnum.DELETED.getCode());
        return this.updateById(tmpBean);
    }
    
    /**
     * 通过UUID查询单条数据
     *
     * @param uuid
     * @return 实例对象
     */
    private FpCarInfoPO getPoByUuid(String uuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        return this.getOne(queryWrapper);
    }
}
