package com.steerinfo.baseinfo.meterbasesystemnews.service.impl;

import cn.hutool.core.lang.Assert;
import com.steerinfo.baseinfo.meterbasesystemmessage.mapper.MeterBaseSystemMessageMapper;
import com.steerinfo.baseinfo.meterbasesystemmessage.model.MeterBaseSystemMessage;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasesystemnews.model.MeterBaseSystemNews;
import com.steerinfo.baseinfo.meterbasesystemnews.mapper.MeterBaseSystemNewsMapper;
import com.steerinfo.baseinfo.meterbasesystemnews.service.IMeterBaseSystemNewsService;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.meterwork.meterworkhookactfirst.model.MeterWorkHookActFirst;
import com.steerinfo.util.StringUtils;
import io.micrometer.core.instrument.Meter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MeterBaseSystemNews服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2022-07-25 09:45
 * 类描述
 * 修订历史：
 * 日期：2022-07-25
 * 作者：generator
 * 参考：
 * 描述：MeterBaseSystemNews服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterBaseSystemNewsService")
public class MeterBaseSystemNewsServiceImpl extends BaseServiceImpl<MeterBaseSystemNews, String> implements IMeterBaseSystemNewsService {

    @Resource
    private MeterBaseSystemNewsMapper meterBaseSystemNewsMapper;

    @Resource
    private MeterBaseSystemMessageMapper meterBaseSystemMessageMapper;

    @Override
    protected IBaseMapper<MeterBaseSystemNews, String> getMapper() {
        return meterBaseSystemNewsMapper;
    }

    @Override
    public RESTfulResult addNews(HashMap<String, Object> params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            String carNo = params.get("carNo") == null ? "" : params.get("carNo").toString();
            Assert.isTrue(StringUtils.isNotEmpty(carNo), "车号为空,不允许生成车辆通告信息");
            int count = 0;
            List<MeterBaseSystemMessage> messageList = meterBaseSystemMessageMapper.selectByParameters(null);
            if(messageList.size() >= 1) {
                for(MeterBaseSystemMessage messageModel: messageList) {
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("carNo", carNo);
                    obj.put("systemMessageNo", messageModel.getSystemMessageNo());
                    List<MeterBaseSystemNews> newsList = meterBaseSystemNewsMapper.selectByParameters(obj);
                    if(newsList.size() < 1) {
                        MeterBaseSystemNews model = new MeterBaseSystemNews();
                        model.setCarNewsNo(carNo + messageModel.getSystemMessageNo());
                        model.setCarNo(carNo);
                        model.setSystemMessageNo(messageModel.getSystemMessageNo());
                        model.setIsRead("0");
                        meterBaseSystemNewsMapper.insertSelective(model);
                        count ++;
                    }
                }
                rm.setSucceed();
                rm.setCode("200");
                rm.setData(null);
                rm.setMessage(carNo + "自动生成了" + count + "条车辆通告信息");
            }else {
                rm.setData(null);
                rm.setCode("500");
                rm.setMessage("系统通告没有通告，不能生成车辆通告信息");
            }
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public PageList<MeterBaseSystemNews> newsListDesc(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        String carNo = params.get("carNo") == null ? "" : params.get("carNo").toString();
        Assert.isTrue(StringUtils.isNotEmpty(carNo), "车号为空,不允许生成车辆通告信息");
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("carNo", carNo);
        List<MeterBaseSystemNews> modelList = meterBaseSystemNewsMapper.distinctMessageNo(obj);
        List<String> newsList = modelList.stream().map(MeterBaseSystemNews::getSystemMessageNo).collect(Collectors.toList());
        if(newsList.size() < 1) {
            newsList.add("");
        }
//        HashMap<String, Object> objMap = new HashMap<>();
//        objMap.put("carNo", carNo);
        params.put("newList", newsList);
        List<MeterBaseSystemMessage> messagesList = meterBaseSystemMessageMapper.listDesc(params);
        PageList pageInfo = new PageList(messagesList);
        return pageInfo;
    }
}
