package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.AverageLifetimeMapper;
import com.lightinit.hsdatashow.mapper.PopulationRegionMapper;
import com.lightinit.hsdatashow.mapper.RegionMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.model.ResultStateCode;
import com.lightinit.hsdatashow.model.admin.AverageLifetimeModel;
import com.lightinit.hsdatashow.model.admin.PopulationRegionResultModel;
import com.lightinit.hsdatashow.service.ms.IMSAverageLifetimeService;
import com.lightinit.hsdatashow.service.ms.IMSPopulationRegionService;
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 MSPopulationRegionServiceImpl implements IMSPopulationRegionService {
    @Autowired
    private PopulationRegionMapper populationRegionMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Override
    public List<PopulationRegionResultModel> queryList(BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PopulationRegionExample example = new PopulationRegionExample();
        example.setPage(pager);
        return populationRegionMapper.getPopulationRegion(example);
    }

    @Override
    public long queryCount(BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PopulationRegionExample example = new PopulationRegionExample();example.setPage(pager);

        return populationRegionMapper.countByExample(example);
    }

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

    @Override
    public ResultState add(PopulationRegionResultModel inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        Region region = new Region();
        region.setRegionName(inputModel.getRegionName());
        long sqlResult1 = regionMapper.insert(region);
        PopulationRegion entity = new PopulationRegion();
        entity.setCreatedAt(new Date());
        entity.setQuantity(inputModel.getQuantity());
        entity.setRegionId(region.getId());
        long sqlResult = populationRegionMapper.insert(entity);
        if (!(sqlResult > 0 && sqlResult1 > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

    @Override
    public PopulationRegionResultModel edit(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return populationRegionMapper.selectById(id);
    }

    @Override
    public ResultState doEdit(PopulationRegionResultModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>();
        Region region = new Region();
        BeanUtils.copyProperties(inputModel,region);
        region.setId(inputModel.getRegionId());
        long sqlResult1 = regionMapper.updateByPrimaryKey(region);
        PopulationRegion entity = new PopulationRegion();
        BeanUtils.copyProperties(inputModel,entity);
        entity.setRegionId(region.getId());
        long sqlResult2 = populationRegionMapper.updateByPrimaryKey(entity);
        if (!(sqlResult1>0&&sqlResult2>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(entity.getId());
        return resultState;
    }
}
