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

import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.cplbusiness.entity.DataCrkywCplspDefault;
import com.unis.crk.cplbusiness.entity.DataCrkywCpltzdxxFlt001;
import com.unis.crk.cplbusiness.mapper.DataCrkywCplspDefaultMapper;
import com.unis.crk.cplbusiness.service.IDataCrkywCplspDefaultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.cplbusiness.service.IDataCrkywCpltzdxxFlt001Service;
import com.unis.crk.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.*;

import cn.hutool.json.JSONObject;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplspDefaultVo;

import javax.annotation.Resource;

/**
 * <p>
 * 成品粮商品表 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-03-23
 */
@Service
public class DataCrkywCplspDefaultServiceImpl extends ServiceImpl<DataCrkywCplspDefaultMapper, DataCrkywCplspDefault> implements IDataCrkywCplspDefaultService {

    @Resource
    private DataCrkywCplspDefaultMapper dataCrkywCplspDefaultMapper;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;
    @Autowired
    private IDataCrkywCpltzdxxFlt001Service dataCrkywCpltzdxxFlt001Service;
    @Override
    public JSONObject saveSp(DataCrkywCplspDefault dataCrkywCplspDefault) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        //查询商品编码是否重复
        QueryWrapper<DataCrkywCplspDefault> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sjjlspmcbh",dataCrkywCplspDefault.getSjjlspmcbh());
           if (super.list(queryWrapper).size() > 0 ){
               jsonObject.set("msg", "商品编号已存在");
               return jsonObject;
           }
        dataCrkywCplspDefault.setTccg("0"); //默认为非统筹采购
        dataCrkywCplspDefault.setIsEnable("1"); //默认启用
        if(!super.save(dataCrkywCplspDefault)){
            jsonObject.set("msg", "保存数据失败");
            return jsonObject;
        }
        jsonObject.set("success", true);
        jsonObject.set("msg", "保存成功");
        return jsonObject;
    }

    @Override
    public JSONObject update(DataCrkywCplspDefault dataCrkywCplspDefault) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        if(!super.saveOrUpdate(dataCrkywCplspDefault)){
            jsonObject.set("msg", "修改数据失败");
            return jsonObject;
        }
        jsonObject.set("msg", "修改成功");
        jsonObject.set("success", true);
        return jsonObject;
    }

    @Override
    public JSONObject delete(String id) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        DataCrkywCplspDefault dataCrkywCplspDefault = dataCrkywCplspDefaultMapper.selectById(id);
        if(!super.removeById(id)){
            jsonObject.set("msg", "删除数据失败");
            return jsonObject;
        }

        deleteDataRecordService.save(id,"data_crkyw_cplsp_default",dataCrkywCplspDefault.getUnitid(),new Date());
        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
    }

    /**
     * 按照条件查询成品粮商品表信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DataCrkywCplspDefaultVo pageQuery){
        //条形查询器
        QueryWrapper<DataCrkywCplspDefault> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
            queryWrapper.eq("a.unitid",pageQuery.getUnitId());
        }
        if (StringUtils.isNotBlank(pageQuery.getSjjlppbh())){
            queryWrapper.eq("a.sjjlppbh",pageQuery.getSjjlppbh());
        }
        if (StringUtils.isNotBlank(pageQuery.getSjjlpzbh())){
            queryWrapper.eq("a.sjjlpzbh",pageQuery.getSjjlpzbh());
        }
        if (StringUtils.isNotBlank(pageQuery.getSjjlspmc())){
            queryWrapper.like("a.sjjlspmc", ResultUtil.getQueryWrapperformat(pageQuery.getSjjlspmc()));
        }
        queryWrapper.orderByDesc("cjsj");
        //分页相关
        Page<DataCrkywCplspDefault> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

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

    @Override
    public List<Map<String, Object>> getCplSpList(String unitId) {
        return  dataCrkywCplspDefaultMapper.getCplTzdList(unitId);
    }

    @Override
    public Boolean chekeBh(String sjjlspmcbh) {
        //条形查询器
        QueryWrapper<DataCrkywCplspDefault> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sjjlspmcbh)){
            queryWrapper.eq("sjjlspmcbh",sjjlspmcbh);
        }

        List<DataCrkywCplspDefault> dataCrkywCplspDefaults = dataCrkywCplspDefaultMapper.selectList(queryWrapper);
        if(dataCrkywCplspDefaults.size()>0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List<DataCrkywCplspDefault> getBySpbhs(List<String> spbhs) {
        QueryWrapper<DataCrkywCplspDefault> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("sjjlspmcbh",spbhs);
        List<DataCrkywCplspDefault> dataCrkywCplspDefaults = dataCrkywCplspDefaultMapper.selectList(queryWrapper);
        return dataCrkywCplspDefaults;
    }

    @Override
    public  List<Map<String,Object>> getCplSpListbydd(String ddId) {
        QueryWrapper<DataCrkywCpltzdxxFlt001> queryWrappertzdxx = new QueryWrapper<>();
        queryWrappertzdxx.eq("MainDataID",ddId);
        List<DataCrkywCpltzdxxFlt001> fltForMainDataId = dataCrkywCpltzdxxFlt001Service.list(queryWrappertzdxx);
        if(fltForMainDataId.size()>0){
            Iterator<DataCrkywCpltzdxxFlt001> iterator = fltForMainDataId.iterator();
            ArrayList<String> strings = new ArrayList<>();
            while (iterator.hasNext()){
                DataCrkywCpltzdxxFlt001 next = iterator.next();
                strings.add(next.getMxpz());
            }
            QueryWrapper<DataCrkywCplspDefault> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("sjjlspmcbh",strings);
            List<Map<String, Object>> cplSpListbydd = dataCrkywCplspDefaultMapper.getCplSpListbydd(queryWrapper);
            return  cplSpListbydd;
        }else {
            return  null;
        }
    }


}
