/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.unis.grainReserves.modular.grReserveScale.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.basic.info.modular.mq.dto.jkEnum;
import com.unis.basic.info.modular.mq.pub.RecPublishService;
import com.unis.basic.info.modular.unitInfo.entity.UnitInfo;
import com.unis.basic.info.modular.unitInfo.mapper.UnitInfoMapper;
import com.unis.basic.info.modular.warehouseInfo.param.WarehouseInfoIdParam;
import com.unis.common.pojo.CommonValidList;
import com.unis.grainReserves.modular.grReserveScale.param.*;
import com.unis.grainReserves.modular.grReserveScale.vo.GrReserveScaleVO;
import com.unis.grainReserves.modular.grReserveScale.vo.LspzInfoVO;
import com.unis.grainReserves.modular.grReserveScale.vo.LsxzInfoVO;
import com.unis.grainReserves.modular.grReserveScale.vo.OrgInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.unis.common.enums.CommonSortOrderEnum;
import com.unis.common.exception.CommonException;
import com.unis.common.page.CommonPageRequest;
import com.unis.grainReserves.modular.grReserveScale.entity.GrReserveScale;
import com.unis.grainReserves.modular.grReserveScale.mapper.GrReserveScaleMapper;
import com.unis.grainReserves.modular.grReserveScale.service.GrReserveScaleService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Date;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 粮食储备-储备规模Service接口实现类
 *
 * @author hjc
 * @date 2024/05/14 17:28
 **/
@Service
public class GrReserveScaleServiceImpl extends ServiceImpl<GrReserveScaleMapper, GrReserveScale> implements GrReserveScaleService {

    @Autowired
    private UnitInfoMapper unitInfoMapper;

    @Autowired
    private GrReserveScaleMapper grReserveScaleMapper;

    /*@Resource
    private RestTemplate restTemplate;*/

    @Resource
    private RecPublishService recPublishService;

    @Value("${jsgjjsjUrl}")
    private String jsgjjsjUrl;

    @Override
    public Page<GrReserveScaleVO> page(GrReserveScalePageParam pageParam) {
       /* QueryWrapper<GrReserveScale> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isAllNotEmpty(grReserveScalePageParam.getSortField(), grReserveScalePageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(grReserveScalePageParam.getSortOrder());
            queryWrapper.orderBy(true, grReserveScalePageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(grReserveScalePageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(GrReserveScale::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);*/

        Page<GrReserveScaleVO> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return grReserveScaleMapper.selectMyPage(page,pageParam.getUnitInfoName(),
                pageParam.getGrainNatureName(),pageParam.getGrainVarietyName());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(GrReserveScaleAddParam addParam) {
        GrReserveScale grReserveScale = BeanUtil.toBean(addParam, GrReserveScale.class);

        this.save(grReserveScale);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(GrReserveScaleEditParam grReserveScaleEditParam) {
        GrReserveScale grReserveScale = this.queryEntity(grReserveScaleEditParam.getId());
        BeanUtil.copyProperties(grReserveScaleEditParam, grReserveScale);
        this.updateById(grReserveScale);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<GrReserveScaleIdParam> grReserveScaleIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(grReserveScaleIdParamList, GrReserveScaleIdParam::getId));

    }

    @Override
    public GrReserveScaleVO detail(GrReserveScaleIdParam grReserveScaleIdParam) {
        //return this.queryEntity(grReserveScaleIdParam.getId());

        return grReserveScaleMapper.detail(grReserveScaleIdParam.getId());


    }

    @Override
    public GrReserveScale queryEntity(String id) {
        GrReserveScale grReserveScale = this.getById(id);
        if (ObjectUtil.isEmpty(grReserveScale)) {
            throw new CommonException("粮食储备-储备规模不存在，id值为：{}", id);
        }
        return grReserveScale;
    }

    /**
     * 获取粮食储备-企业单位
     *
     * @author hjc
     * @date 2024/05/14 17:28
     */
    @Override
    public List<UnitInfo> getUnitInfo() {
        List<UnitInfo> unitInfos = unitInfoMapper.selectList(null);
        if (CollectionUtils.isEmpty(unitInfos)) {
            throw new CommonException("暂无企业单位数据");
        }
        return unitInfos;

    }

    /**
     * 获取粮食储备-根据企业信息查出库区名称
     *
     * @author hjc
     * @date  2024/05/14 17:28
     */
    @Override
    public List<OrgInfoVO> getOrgInfo(OrgInfoParam orgInfoParam) {

       return grReserveScaleMapper.selectOrgInfo(orgInfoParam.getId());

    }
    /**
     * 获取粮食储备-粮食品种
     *
     * @author hjc
     * @date  2024/05/14 17:28
     */
    @Override
    public List<LspzInfoVO> getLspzInfo() {
        return grReserveScaleMapper.selectLspzInfo();
    }

    /**
     * 获取粮食储备-粮食性质
     *
     * @author hjc
     * @date  2024/05/14 17:28
     */
    @Override
    public List<LsxzInfoVO> getLsxzInfo(String parentId) {
        return grReserveScaleMapper.getLsxzInfo(parentId);
    }

    /* *
     * 获取粮食储备-下发
     *
     * @author hjc
     * @date  2024/05/14 17:28
     * */
    @Override
    public void issued( CommonValidList<GrReserveScaleIdAndStatusParam> paramCommonValidList) {

        for (GrReserveScaleIdAndStatusParam grReserveScaleIdParam : paramCommonValidList) {

            String id = grReserveScaleIdParam.getId();
            GrReserveScale grReserveScale = grReserveScaleMapper.selectById(id);
            if (ObjectUtils.isEmpty(grReserveScale)){
                throw new CommonException("暂无此id数据，id为{}",id);
            }

            //设置下发状态
            grReserveScale.setStatus(grReserveScaleIdParam.getStatus());
            grReserveScale.setDistributeTime(new Date());

            grReserveScaleMapper.updateById(grReserveScale);

            recPublishService.sendMessage("i", jkEnum.cbgm.getTableName(),id+"",grReserveScale.getOrgInfoId());

        }
    }

    /**
     * 国家局数据
     */
    /*public void gjjjk(String id, Integer orgId,String czbz) {

        String tableName = "ODS_UP_CBGMXXJK";
        *//**
         * restTemplate调用
         *//*
        //String url = "http://localhost:9022/recPublish/sendMessage";//
        //发送post请求，并打印结果，以String类型接收响应结果JSON字符串

        MultiValueMap<String, Object> sendMap = new LinkedMultiValueMap<>();
        sendMap.add("tableName", tableName);
        sendMap.add("czbz", czbz);
        sendMap.add("ywId", id);
        sendMap.add("orgId", orgId);
    }*/
}
