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

import cn.hutool.core.lang.Assert;
import cn.hutool.db.Page;
import com.steerinfo.baseinfo.meterbaseuserprintconfig.mapper.MeterBaseUserPrintConfigMapper;
import com.steerinfo.baseinfo.meterbaseuserprintconfig.model.MeterBaseUserPrintConfig;
import com.steerinfo.baseinfo.meterbaseuserprintconfig.service.IMeterBaseUserPrintConfigService;
import com.steerinfo.baseinfo.meterbaseuserspotconfig.model.MeterBaseUserSpotConfig;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.util.PublicMethod;
import okhttp3.internal.Internal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired(required = false)
    private MeterBaseUserPrintConfigMapper meterBaseUserPrintConfigMapper;

    @Override
    protected IBaseMapper<MeterBaseUserPrintConfig, String> getMapper() {
        return meterBaseUserPrintConfigMapper;
    }

    @Override
    public List<MeterBaseUserPrintConfig> noPage(HashMap<String, Object> params) {
        List<MeterBaseUserPrintConfig> rows = meterBaseUserPrintConfigMapper.likeByDesc(params);
        return rows;
    }

    @Override
    public PageList<MeterBaseUserPrintConfig> likeByDesc(HashMap<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<MeterBaseUserPrintConfig> rows = meterBaseUserPrintConfigMapper.likeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public MeterBaseUserPrintConfig addModel(MeterBaseUserPrintConfig model) {
        Assert.isTrue(model != null, "用户物料收发货单位配置信息不能为空，请重试");
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setPrintId(PublicMethod.IdCreate(3));
        model.setCreateManNo(userId);
        model.setCreateManName(userName);
        model.setCreateTime(new Date());
//        model.setValueFlag("1");
        meterBaseUserPrintConfigMapper.insert(model);
        return model;
    }

    @Override
    public MeterBaseUserPrintConfig updateModel(MeterBaseUserPrintConfig model) {
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setUpdateManNo(userId);
        model.setUpdateManName(userName);
        model.setUpdateTime(new Date());
        meterBaseUserPrintConfigMapper.updateByPrimaryKeySelective(model);
        return meterBaseUserPrintConfigMapper.selectByPrimaryKey(model.getPrintId());
    }
}
