package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.mapper.AverageLifetimeMapper;
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.service.ms.IMSAverageLifetimeService;
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 MSAverageLifetimeServiceImpl implements IMSAverageLifetimeService {
    @Autowired
    private AverageLifetimeMapper averageLifetimeMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Override
    public List<AverageLifetimeModel> queryList(BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AverageLifetimeExample example = new AverageLifetimeExample();
        example.setPage(pager);
        return averageLifetimeMapper.getLifetime(example);
    }

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

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

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

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

    @Override
    public ResultState doEdit(AverageLifetimeModel 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);
        AverageLifetime entity = new AverageLifetime();
        BeanUtils.copyProperties(inputModel,entity);
        entity.setRegionId(region.getId());
        long sqlResult2 = averageLifetimeMapper.updateByPrimaryKey(entity);
        if (!(sqlResult1>0&&sqlResult2>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(entity.getId());
        return resultState;
    }
}
