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

import com.unis.crk.basic.entity.ReceiveUnit;
import com.unis.crk.basic.mapper.ReceiveUnitMapper;
import com.unis.crk.basic.service.IReceiveUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
import cn.hutool.json.JSONObject;
import com.unis.crk.basic.entity.vo.ReceiveUnitVo;

import javax.annotation.Resource;

/**
 * <p>
 * 收发单位 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2023-05-12
 */
@Service
public class ReceiveUnitServiceImpl extends ServiceImpl<ReceiveUnitMapper, ReceiveUnit> implements IReceiveUnitService {

    @Resource
    private ReceiveUnitMapper receiveUnitMapper;

    /**
     *保存收发单位
     * @param entity 收发单位业务数据
     * @return
     */
    @Override
    public JSONObject saveT(ReceiveUnit entity){
        JSONObject jsonObject = new JSONObject();
        boolean b = super.save(entity);
        jsonObject.set("msg",b?"保存成功":"保存失败");
        jsonObject.set("success",b);
        return jsonObject;
    }
    /**
     *修改收发单位
     * @param entity 收发单位业务数据
     * @return
     */
    @Override
    public JSONObject update(ReceiveUnit entity){
        JSONObject jsonObject = new JSONObject();
        boolean b =  super.updateById(entity);
        jsonObject.set("msg",b?"修改成功":"修改失败");
        jsonObject.set("success",b);
        return jsonObject;
    }
    /**
     *删除收发单位数据
     * @param id 收发单位主键尖
     * @return
     */
    @Override
    public JSONObject delete(String id){
        JSONObject jsonObject = new JSONObject();
        boolean b = false;
        if (StringUtils.isNotBlank(id)){
        b =  super.removeById(id);
        }
        jsonObject.set("msg",b?"删除成功":"删除失败");
        jsonObject.set("success",b);
        return jsonObject;
    }

    /**
     * 按照条件查询收发单位信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(ReceiveUnitVo pageQuery){
        {
        //条形查询器
        QueryWrapper<ReceiveUnit> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(pageQuery.getUnitId())) {
                queryWrapper.eq("UNIT_ID", pageQuery.getUnitId());
            }
        //分页相关
        Page<ReceiveUnit> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

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

}
