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.common.service.IDeleteDataRecordService;
import com.unis.crk.cplbusiness.entity.DispatcPersonnelManagement;
import com.unis.crk.cplbusiness.entity.DispatchEhicleManagement;
import com.unis.crk.cplbusiness.entity.vo.DispatcPersonnelManagementVo;
import com.unis.crk.cplbusiness.mapper.DispatcPersonnelManagementMapper;
import com.unis.crk.cplbusiness.service.IDispatcPersonnelManagementService;
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 DispatcPersonnelManagementServiceImpl extends ServiceImpl<DispatcPersonnelManagementMapper, DispatcPersonnelManagement> implements IDispatcPersonnelManagementService {

    @Resource
    private DispatcPersonnelManagementMapper dispatcPersonnelManagementMapper;

    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;

    /**
     *修改调度管理(人员管理表)
     * @param param 调度管理(人员管理表)业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(DispatcPersonnelManagement param){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        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);
        DispatcPersonnelManagement dispatcPersonnelManagement = dispatcPersonnelManagementMapper.selectById(id);
        if(!super.removeById(id)){
            jsonObject.set("msg", "删除数据失败");
            return jsonObject;
        }
        deleteDataRecordService.save(id,"dispatc_personnel_management",dispatcPersonnelManagement.getUnitid(),new Date());
        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
        }

    /**
     * 按照条件查询调度管理(人员管理表)信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DispatcPersonnelManagementVo pageQuery){
        {
        //条形查询器
        QueryWrapper<DispatcPersonnelManagement> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotBlank(pageQuery.getUnitId())) {
                queryWrapper.eq("unitid", pageQuery.getUnitId());
            }
        if(StrUtil.isNotBlank(pageQuery.getPersonName())){
            queryWrapper.like("person_name", ResultUtil.getQueryWrapperformat(pageQuery.getPersonName()));
        }
        if(StrUtil.isNotBlank(pageQuery.getContactNumber())){
            queryWrapper.like("contact_number",ResultUtil.getQueryWrapperformat(pageQuery.getContactNumber()));
        }

        //分页相关
        Page<DispatcPersonnelManagement> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

        IPage<Map<String,Object>> page = dispatcPersonnelManagementMapper.selectMyMapsPage(p,queryWrapper);
        return page;
        }
    }
    /**
     * 按照条件查询调度管理(人员管理表)信息
     * @throws Exception
     */
    @Override
    public List<DispatcPersonnelManagement> getList(){
        {
            QueryWrapper<DispatcPersonnelManagement> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("dataId","person_name","age");
            queryWrapper.orderByDesc("builderDate");
            List<DispatcPersonnelManagement> dispatcPersonnelManagements = dispatcPersonnelManagementMapper.selectList(queryWrapper);

            return dispatcPersonnelManagements;
        }
    }

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

        List<DispatcPersonnelManagement> dispatcPersonnelManagements = dispatcPersonnelManagementMapper.selectList(queryWrapper);

        return dispatcPersonnelManagements;
    }


}
