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.cplbusiness.entity.DataCrkywCpljyxxDefault;
import com.unis.crk.cplbusiness.entity.DataCrkywCplpfDefault;
import com.unis.crk.cplbusiness.entity.DataCrkywCpltzdxxFlt001;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplpfDefaultVo;
import com.unis.crk.cplbusiness.mapper.DataCrkywCplpfDefaultMapper;
import com.unis.crk.cplbusiness.service.IDataCrkywCplpfDefaultService;
import com.unis.crk.cplbusiness.service.IDataCrkywCpltzdxxDefaultService;
import com.unis.crk.cplbusiness.service.IDataCrkywCpltzdxxFlt001Service;
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.List;
import java.util.Map;

/**
 * <p>
 * 成品粮评分 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-04-25
 */
@Service
public class DataCrkywCplpfDefaultServiceImpl extends ServiceImpl<DataCrkywCplpfDefaultMapper, DataCrkywCplpfDefault> implements IDataCrkywCplpfDefaultService {

    @Resource
    private DataCrkywCplpfDefaultMapper dataCrkywCplpfDefaultMapper;
    @Autowired
    private IDataCrkywCpltzdxxDefaultService iDataCrkywCpltzdxxDefaultService;
    @Autowired
    private IDataCrkywCpltzdxxFlt001Service dataCrkywCpltzdxxFlt001Service;
    /**
     *修改成品粮评分
     * @param param 成品粮评分业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(DataCrkywCplpfDefault param){
        return null;
    }
    /**
     *删除成品粮评分数据
     * @param id 成品粮评分主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject delete(String id){
        return null;
        }

    /**
     * 按照条件查询成品粮评分信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DataCrkywCplpfDefaultVo pageQuery){
        {
        //条形查询器
            iDataCrkywCpltzdxxDefaultService.findPagepfDefaultVo(pageQuery);
        QueryWrapper<DataCrkywCplpfDefault> queryWrapper = new QueryWrapper<>();
        //分页相关
        Page<DataCrkywCplpfDefault> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

        IPage<Map<String,Object>> page = dataCrkywCplpfDefaultMapper.selectMyMapsPage(p,queryWrapper);
            List<Map<String, Object>> records = page.getRecords();
            if(records !=null && records.size()>0){
                for (int i=0;i<=records.size();i++){
                    Map<String, Object> stringObjectMap = records.get(i);
                    String dataID = stringObjectMap.get("DataID").toString();
                    QueryWrapper<DataCrkywCplpfDefault> queryWrapperpf = new QueryWrapper<>();
                    queryWrapperpf.like("tzdbm", ResultUtil.getQueryWrapperformat(dataID));
                    queryWrapperpf.apply("zlfens  is not null or lyfs  is not null");
                    List<DataCrkywCplpfDefault> dataCrkywCplpfDefaults = this.dataCrkywCplpfDefaultMapper.selectList(queryWrapperpf);
                    if(dataCrkywCplpfDefaults !=null && dataCrkywCplpfDefaults.size()>0){
                        records.get(i).put("sfpf","已评分");
                    }else{
                        records.get(i).put("sfpf","未评分");
                    }
                }
            }else{

            }
        return page;
        }
    }

    @Override
    public IPage<Map<String, Object>> findPagepfDefaultVo(DataCrkywCplpfDefaultVo pageQuery) {
        //条形查询器
        IPage<Map<String, Object>> pagepfDefaultVo = iDataCrkywCpltzdxxDefaultService.findPagepfDefaultVo(pageQuery);
        List<Map<String, Object>> records = pagepfDefaultVo.getRecords();
        if(records !=null && records.size()>0){
            for (int i=0;i<records.size();i++){
                Map<String, Object> stringObjectMap = records.get(i);
                String dataID = stringObjectMap.get("tzdbm").toString();
                QueryWrapper<DataCrkywCplpfDefault> queryWrapperpf = new QueryWrapper<>();
                queryWrapperpf.eq("tzdbm",ResultUtil.getQueryWrapperformat(dataID));
                queryWrapperpf.apply(" (zlfens  is not null or lyfs  is not null)");
                List<DataCrkywCplpfDefault> dataCrkywCplpfDefaults = this.dataCrkywCplpfDefaultMapper.selectList(queryWrapperpf);
                if(dataCrkywCplpfDefaults !=null && dataCrkywCplpfDefaults.size()>0){
                    records.get(i).put("sfpf","已评分");
                }else{
                    records.get(i).put("sfpf","未评分");
                }

                List<DataCrkywCpltzdxxFlt001> dataCrkywCplckFlt001s = dataCrkywCpltzdxxFlt001Service.getFltForMainDataId(stringObjectMap.get("dataid").toString());
                String mxpzValue = "";
                for (int j = 0; j < dataCrkywCplckFlt001s.size(); j++) {
                    Map<String,Object> dataCrkywCplckFlt001 = (Map<String,Object>) dataCrkywCplckFlt001s.get(j);
                    mxpzValue+= dataCrkywCplckFlt001.getOrDefault("mxpzmc","")+",";
                }
                records.get(i).put("mxpzValue",StrUtil.sub(mxpzValue,0,mxpzValue.length()-1));
            }
        }else{

        }
        return pagepfDefaultVo;
    }

    @Override
    public IPage<Map<String,Object>> findPagepfDefault(DataCrkywCplpfDefaultVo pageQuery) {
        QueryWrapper<DataCrkywCplpfDefault> queryWrapperpf = new QueryWrapper<>();
        if(StringUtils.isNotBlank(pageQuery.getUnitId())){
            queryWrapperpf.eq("a.unitid",ResultUtil.getQueryWrapperformat(pageQuery.getUnitId()));
        }
        if(StringUtils.isNotBlank(pageQuery.getTzdbm())){
            queryWrapperpf.like("tzdbm",ResultUtil.getQueryWrapperformat(pageQuery.getTzdbm()));
        }
        Page<DataCrkywCplpfDefault> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());
        IPage<Map<String, Object>> mapIPage = dataCrkywCplpfDefaultMapper.selectMyMapsPage(p, queryWrapperpf);

        return mapIPage;
    }


}
