package com.steerinfo.meterwork.meterworkshiftreport.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.meterworkshiftreport.mapper.MeterWorkShiftReportMapper;
import com.steerinfo.meterwork.meterworkshiftreport.model.MeterWorkShiftReport;
import com.steerinfo.meterwork.meterworkshiftreport.service.IMeterWorkShiftReportService;
import com.steerinfo.util.PublicMethod;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private MeterWorkShiftReportMapper meterWorkShiftReportMapper;

    @Override
    protected IBaseMapper<MeterWorkShiftReport, String> getMapper() {
        return meterWorkShiftReportMapper;
    }

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

    @Override
    public MeterWorkShiftReport updateModel(MeterWorkShiftReport 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);
        meterWorkShiftReportMapper.updateByPrimaryKeySelective(model);
        return meterWorkShiftReportMapper.selectByPrimaryKey(model.getShiftReportId());
    }

    @Override
    public MeterWorkShiftReport addModel(MeterWorkShiftReport 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);
        meterWorkShiftReportMapper.insert(model);
        return model;
    }
}
