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

import com.steerinfo.baseinfo.meterbaserailwaycarriageweight.model.MeterBaseRailwayCarriageWeight;
import com.steerinfo.baseinfo.meterbaseuserspot.model.MeterBaseUserSpot;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasegroupspot.model.MeterBaseGroupSpot;
import com.steerinfo.baseinfo.meterbasegroupspot.mapper.MeterBaseGroupSpotMapper;
import com.steerinfo.baseinfo.meterbasegroupspot.service.IMeterBaseGroupSpotService;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
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;

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

    @Resource
    private MeterBaseGroupSpotMapper meterBaseGroupSpotMapper;

    @Override
    protected IBaseMapper<MeterBaseGroupSpot, String> getMapper() {
        return meterBaseGroupSpotMapper;
    }

    @Override
    public String creatId() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
        return afl + meterBaseGroupSpotMapper.GetNewID(afl);
    }

    @Override
    public PageList<MeterBaseGroupSpot> queryByGroupSpotForPage(HashMap<String, Object> map, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseGroupSpot> rows = meterBaseGroupSpotMapper.queryByGroupSpot(map);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<MeterBaseGroupSpot> noPage(HashMap<String, Object> map) {
        List<MeterBaseGroupSpot> rows = meterBaseGroupSpotMapper.queryByGroupSpot(map);
        return rows;
    }

    @Override
    public RESTfulResult checkAdd(List<MeterBaseGroupSpot> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("200");
        UserPayload userPayload = UserPayload.getCurrUser();
        String userId = userPayload.getUserCode();
        String userName = userPayload.getUserName();
        try{
            int num = 0;
            for(MeterBaseGroupSpot model: models) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String afl = sdf.format(new Date());
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("groupId", model.getGroupId());
                obj.put("spotId", model.getSpotId());
                List<MeterBaseGroupSpot> groupSpots =  meterBaseGroupSpotMapper.selectByParameters(obj);
                if(groupSpots.size() < 1) {
                    model.setGroupSpotId(sdf.format(new Date()) + meterBaseGroupSpotMapper.GetNewID(afl));
                    model.setValueFlag("1");
                    model.setCreateManNo(userId);
                    model.setCreateManName(userName);
                    model.setCreateTime(new Date());
                    meterBaseGroupSpotMapper.insertSelective(model);
                }else {
                    groupSpots.get(0).setUpdateManNo(userId);
                    groupSpots.get(0).setUpdateManName(userName);
                    groupSpots.get(0).setUpdateTime(new Date());
                    groupSpots.get(0).setValueFlag("1");
                    meterBaseGroupSpotMapper.updateByPrimaryKeySelective(groupSpots.get(0));
                }
                num++;
            }
            rm.setData(num);
            rm.setMessage("操作成功");
        }
        catch (Exception e){
            e.printStackTrace();
            rm.setCode("500");
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult remove(List<MeterBaseGroupSpot> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("200");
        try{
            int num = 0;
            for(MeterBaseGroupSpot model: models) {
                meterBaseGroupSpotMapper.deleteByPrimaryKey(model.getGroupSpotId());
                num++;
            }
            rm.setData(num);
            rm.setMessage("操作成功");
        }
        catch (Exception e){
            e.printStackTrace();
            rm.setCode("500");
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public void deleteByLogic(MeterBaseGroupSpot meterBaseRailwayCarriageWeight) {
        meterBaseGroupSpotMapper.updateByPrimaryKeySelective(meterBaseRailwayCarriageWeight);
    }

    @Override
    public MeterBaseGroupSpot queryById(String id) {
        return meterBaseGroupSpotMapper.selectByPrimaryKey(id);
    }
}
