package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.AverageExpectedRateMapper;
import com.lightinit.hsdatashow.mapper.PopulationSummaryMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ResultStateCode;
import com.lightinit.hsdatashow.service.ms.IMSAverageExpectedRateService;
import com.lightinit.hsdatashow.service.ms.IMSPeopleGrowthService;
import com.lightinit.hsdatashow.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class MSAverageExpectedRateServiceImpl implements IMSAverageExpectedRateService {
    @Autowired
    private AverageExpectedRateMapper averageExpectedRateMapper;
    @Override
    public List<AverageExpectedRate> queryList(BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AverageExpectedRateExample example = new AverageExpectedRateExample();
        example.setPage(pager);
        return averageExpectedRateMapper.selectByExample(example);
    }

    @Override
    public long queryCount(BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AverageExpectedRateExample example = new AverageExpectedRateExample();
        example.setPage(pager);
        return averageExpectedRateMapper.countByExample(example);
    }

    @Override
    public ResultState<Long> delete(long id) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        if (!(averageExpectedRateMapper.deleteByPrimaryKey(id)>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return resultState;
    }

    @Override
    public ResultState add(AverageExpectedRate inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        AverageExpectedRate entity = new AverageExpectedRate();
        BeanUtils.copyProperties(inputModel, entity);
        long sqlResult = averageExpectedRateMapper.insert(entity);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

    @Override
    public AverageExpectedRate edit(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return averageExpectedRateMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultState doEdit(AverageExpectedRate inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        if (!(averageExpectedRateMapper.updateByPrimaryKey(inputModel)>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }
}
