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

import com.steerinfo.baseinfo.meterbaseunitdirect.model.MeterBaseUnitDirect;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasesystemmessage.model.MeterBaseSystemMessage;
import com.steerinfo.baseinfo.meterbasesystemmessage.mapper.MeterBaseSystemMessageMapper;
import com.steerinfo.baseinfo.meterbasesystemmessage.service.IMeterBaseSystemMessageService;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Resource
    private MeterBaseSystemMessageMapper meterBaseSystemMessageMapper;

    @Override
    protected IBaseMapper<MeterBaseSystemMessage, String> getMapper() {
        return meterBaseSystemMessageMapper;
    }

    @Override
    public String checkandadd(MeterBaseSystemMessage model) {
        if (StringUtils.isEmpty(model.getSystemMessageName())) {
            return "公告名称不能为空";
        }
        if (StringUtils.isEmpty(model.getSystemMessageContent())){
            return "公告内容不能为空";
        }
        String no = meterBaseSystemMessageMapper.GetNewID();
        model.setSystemMessageNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setCreateManNo(userId);
        model.setCreateManName(userName);
        model.setCreateTime(new Date());
        model.setVaildFlag("1");//有效
        meterBaseSystemMessageMapper.insert(model);
        return "";
    }

    @Override
    public PageList<MeterBaseSystemMessage> likeDesc(HashMap params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseSystemMessage> rows = meterBaseSystemMessageMapper.likeDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public MeterBaseSystemMessage getNews() {
        List<MeterBaseSystemMessage> rows = meterBaseSystemMessageMapper.getNews();
        return rows.get(0);
    }
}
