package com.unis.crk.cplbusiness.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.basic.entity.Attaches;
import com.unis.crk.basic.service.IAttachesService;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.cplbusiness.entity.DispatchEhicleManagement;
import com.unis.crk.cplbusiness.entity.vo.DispatchEhicleManagementVo;
import com.unis.crk.cplbusiness.mapper.DispatchEhicleManagementMapper;
import com.unis.crk.cplbusiness.service.IDispatchEhicleManagementService;
import com.unis.crk.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 调度管理(车辆管理表) 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-05-23
 */
@Service
public class DispatchEhicleManagementServiceImpl extends ServiceImpl<DispatchEhicleManagementMapper, DispatchEhicleManagement> implements IDispatchEhicleManagementService {

    @Resource
    private DispatchEhicleManagementMapper dispatchEhicleManagementMapper;

    @Autowired
    private IAttachesService attachesService;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;
    /**
     *修改调度管理(车辆管理表)
     * @param param 调度管理(车辆管理表)业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(DispatchEhicleManagement param){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        String licensePlateNumber = param.getLicensePlateNumber();
        //车牌号不能重
        QueryWrapper<DispatchEhicleManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("license_plate_number",licensePlateNumber);
        queryWrapper.ne("dataId",param.getDataId());
        List<DispatchEhicleManagement> dispatchEhicleManagements = super.list(queryWrapper);
        if (dispatchEhicleManagements.size() > 0){
            jsonObject.set("msg", "车牌号已存在");
            return jsonObject;
        }
        if(!super.saveOrUpdate(param)){
            jsonObject.set("msg", "修改数据失败");
            return jsonObject;
        }
        jsonObject.set("success", true);
        jsonObject.set("msg", "修改成功");
        return jsonObject;
    }
    /**
     *删除调度管理(车辆管理表)数据
     * @param id 调度管理(车辆管理表)主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject delete(String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        DispatchEhicleManagement dispatchEhicleManagement = dispatchEhicleManagementMapper.selectById(id);
        if(!super.removeById(id)){
            jsonObject.set("msg", "删除数据失败");
            return jsonObject;
        }
        deleteDataRecordService.save(id,"dispatch_ehicle_management",dispatchEhicleManagement.getUnitid(),new Date());
        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
        }

    /**
     * 按照条件查询调度管理(车辆管理表)信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DispatchEhicleManagementVo pageQuery){
        {
        //条形查询器
        QueryWrapper<DispatchEhicleManagement> queryWrapper = new QueryWrapper<>();
        //分页相关
        Page<DispatchEhicleManagement> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());
            if(StringUtils.isNotBlank(pageQuery.getUnitId())) {
                queryWrapper.eq("unitid", pageQuery.getUnitId());
            }
        if(StrUtil.isNotBlank(pageQuery.getLicensePlateNumber())){
            queryWrapper.like("license_plate_number", ResultUtil.getQueryWrapperformat(pageQuery.getLicensePlateNumber()));
        }
        if (StringUtils.isNotBlank(pageQuery.getYwStart()) && StringUtils.isNotBlank(pageQuery.getYwEnd())){
            queryWrapper.apply(" DATE_FORMAT(ifnull(purchase_time,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getYwStart()+"' AND DATE_FORMAT(ifnull(purchase_time,NOW()),'%Y-%m-%d') <= '"+pageQuery.getYwEnd()+" 23:59:59'");

        }
        IPage<Map<String,Object>> page = dispatchEhicleManagementMapper.selectMyMapsPage(p,queryWrapper);
        return page;
        }
    }

    @Override
    public DispatchEhicleManagement getOneById(String id) {
        DispatchEhicleManagement dispatchEhicleManagement = dispatchEhicleManagementMapper.selectById(id);
        List<Attaches> attachesByReferencesID = attachesService.getAttachesByReferencesID(id);
        dispatchEhicleManagement.setAttaches(attachesByReferencesID);

        return dispatchEhicleManagement;
    }

    @Override
    public List<DispatchEhicleManagement> queryDrop(String unitid) {
        QueryWrapper<DispatchEhicleManagement> queryWrapper = new QueryWrapper<>();
        //分页相关
        queryWrapper.select("dataId,license_plate_number");
            queryWrapper.eq("unitid", unitid);

        List<DispatchEhicleManagement> dispatchEhicleManagements = dispatchEhicleManagementMapper.selectList(queryWrapper);

        return dispatchEhicleManagements;
    }


}
