package com.steerinfo.meterwork.meterworkshiftreportmeasurement.service.impl;

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.meterwork.meterworkshiftreportgroup.model.MeterWorkShiftReportGroup;
import com.steerinfo.meterwork.meterworkshiftreportmeasurement.mapper.MeterWorkShiftReportMeasurementMapper;
import com.steerinfo.meterwork.meterworkshiftreportmeasurement.model.MeterWorkShiftReportMeasurement;
import com.steerinfo.meterwork.meterworkshiftreportmeasurement.service.IMeterWorkShiftReportMeasurementService;
import com.steerinfo.util.PublicMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private MeterWorkShiftReportMeasurementMapper meterWorkShiftReportMeasurementMapper;

    @Override
    protected IBaseMapper<MeterWorkShiftReportMeasurement, String> getMapper() {
        return meterWorkShiftReportMeasurementMapper;
    }

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

    @Override
    public MeterWorkShiftReportMeasurement updateModel(MeterWorkShiftReportMeasurement model) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.format(model.getCreateTime());
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setWorkRecorderNo(userId);
        model.setWorkRecorderName(userName);
        meterWorkShiftReportMeasurementMapper.updateByPrimaryKeySelective(model);
        return meterWorkShiftReportMeasurementMapper.selectByPrimaryKey(model.getShiftReportId());
    }

    @Override
    public MeterWorkShiftReportMeasurement addModel(MeterWorkShiftReportMeasurement model) {
        model.setShiftReportId(PublicMethod.IdCreate(3));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.format(model.getCreateTime());
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setWorkRecorderNo(userId);
        model.setWorkRecorderName(userName);
        meterWorkShiftReportMeasurementMapper.insert(model);
        return model;
    }
}
