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.DataCrkywCplzjjgDefault;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplzjjgDefaultVo;
import com.unis.crk.cplbusiness.mapper.DataCrkywCplzjjgDefaultMapper;
import com.unis.crk.cplbusiness.service.IDataCrkywCplzjjgDefaultService;
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 DataCrkywCplzjjgDefaultServiceImpl extends ServiceImpl<DataCrkywCplzjjgDefaultMapper, DataCrkywCplzjjgDefault> implements IDataCrkywCplzjjgDefaultService {

    @Resource
    private DataCrkywCplzjjgDefaultMapper dataCrkywCplzjjgDefaultMapper;

    /**
     *保存质检机构
     * @param  entity 质检机构业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject add(DataCrkywCplzjjgDefault 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(DataCrkywCplzjjgDefault 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(DataCrkywCplzjjgDefaultVo pageQuery){
        {
        //条形查询器
        QueryWrapper<DataCrkywCplzjjgDefault> queryWrapper = new QueryWrapper<>();

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

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

    @Override
    public List<Map<String, Object>> getZjjgList(String unitId) {
        return  dataCrkywCplzjjgDefaultMapper.getZjggList(unitId);
    }


}
