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

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.cplbusiness.entity.DataCrkywGysDefault;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywGysDefaultVo;
import com.unis.crk.cplbusiness.mapper.DataCrkywGysDefaultMapper;
import com.unis.crk.cplbusiness.service.IDataCrkywGysDefaultService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 供应商 服务实现类
 * </p>
 *
 * @author ghh
 * @since 2022-09-14
 */
@Service
public class DataCrkywGysDefaultServiceImpl extends ServiceImpl<DataCrkywGysDefaultMapper, DataCrkywGysDefault> implements IDataCrkywGysDefaultService {

    @Resource
    private DataCrkywGysDefaultMapper dataCrkywGysDefaultMapper;

    /**
     *保存供应商
     * @param  entity 供应商业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject add(DataCrkywGysDefault entity){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success", false);
        if (!super.save(entity)){
        jsonObject.set("msg", "保存失败");
        return jsonObject;
        }

        jsonObject.set("success", true);
        jsonObject.set("msg", "保存成功");
        return jsonObject;
    }
    /**
     *修改供应商
     * @param entity 供应商
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(DataCrkywGysDefault entity){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success", false);
        if (!super.updateById(entity)){
        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);
        if (!super.removeById(id)){
        jsonObject.set("msg", "删除失败");
        return jsonObject;
        }

        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
        }

    /**
     * 按照条件查询供应商信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DataCrkywGysDefaultVo pageQuery){
        {
        //条形查询器
        QueryWrapper<DataCrkywGysDefault> queryWrapper = new QueryWrapper<>();

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

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

    @Override
    public List<Map<String, Object>> getGysList(String unitId) {
        return  dataCrkywGysDefaultMapper.getGysList(unitId);
    }


}
