package com.lightinit.hsdatashow.impl.ms;

import com.lightinit.hsdatashow.entity.BaseExample;
import com.lightinit.hsdatashow.entity.Cockpit;
import com.lightinit.hsdatashow.entity.CockpitExample;
import com.lightinit.hsdatashow.mapper.CockpitMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.service.ms.IMSCockpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

/**
 * @Author: sunxiang
 * @Date: 2018/8/28 13:35
 * @Description:
 **/
@Service
public class MSCockpitServiceImpl implements IMSCockpitService {

    @Autowired
    private CockpitMapper cockpitMapper;

    @Transactional
    @Override
    public ResultState add(Cockpit cockpit) {
        ResultState resultState = new ResultState();
        try{
            cockpitMapper.insert(cockpit);
            resultState.setMsg("插入成功");
            return resultState;
        }catch(Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if(e.getMessage().indexOf("year_category_unique")!=-1){
                resultState.setStateCode(400);
                resultState.setMsg(cockpit.getDataYear()+cockpit.getCategory()+"已经存在");
                return resultState;
            }
            e.printStackTrace();
            resultState.setStateCode(400);
            resultState.setMsg("修改失败");
            return resultState;
        }
    }

    @Override
    public List<Cockpit> list( BaseExample.Page pager) {
        CockpitExample example = new CockpitExample();
        example.setOrderByClause("data_year desc limit "+pager.getBeginIndex()+","+pager.getPageSize());
        List<Cockpit> list = cockpitMapper.selectByExample(example);
       /* List<MultiFunctionalModel> list = new ArrayList<>();
        MultiFunctionalModel multiFunctionalModel=null;
        for (Cockpit cockpit : cockpits) {
            multiFunctionalModel = new MultiFunctionalModel();
            multiFunctionalModel.setId(cockpit.getId());
            multiFunctionalModel.setYear(cockpit.getDataYear());
            multiFunctionalModel.setName(cockpit.getCategory());
            multiFunctionalModel.setTemp1(cockpit.getQuantity());
            multiFunctionalModel.setTemp2(cockpit.getDegree());
            list.add(multiFunctionalModel);
        }*/
        return list;
    }

    @Override
    public Cockpit findById(Long id) {
        Cockpit cockpit = cockpitMapper.selectByPrimaryKey(id);
       /* MultiFunctionalModel model = new MultiFunctionalModel();
        model.setId(cockpit.getId());
        model.setYear(cockpit.getDataYear());
        model.setName(cockpit.getCategory());
        model.setTemp1(cockpit.getQuantity());
        model.setTemp2(cockpit.getDegree());*/
        return  cockpit;
    }

    @Override
    public long queryCount() {
        return cockpitMapper.countByExample(new CockpitExample());
    }

    @Override
    public ResultState editDo(Cockpit cockpit) {
        ResultState resultState = new ResultState();
        try {
            /*Cockpit cockpit = new Cockpit();
            cockpit.setId(multiFunctionalModel.getId());
            cockpit.setDataYear(multiFunctionalModel.getYear());
            cockpit.setCategory(multiFunctionalModel.getName());
            cockpit.setQuantity((Double) multiFunctionalModel.getTemp1());
            cockpit.setDegree((String) multiFunctionalModel.getTemp2());*/
            cockpitMapper.updateByPrimaryKeySelective(cockpit);
            resultState.setMsg("修改成功");
            return resultState;
        }
        catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if(e.getMessage().indexOf("year_category_unique")!=-1){
                resultState.setStateCode(400);
                resultState.setMsg(cockpit.getDataYear()+cockpit.getCategory()+"已经存在");
                return resultState;
            }
            e.printStackTrace();
            resultState.setStateCode(400);
            resultState.setMsg("修改失败");
            return resultState;
        }
    }

    @Override
    public ResultState delete(Long id) {
        ResultState resultState = new ResultState();
        int i=cockpitMapper.deleteByPrimaryKey(id);
        if(i<1){
            resultState.setStateCode(400);
            resultState.setMsg("删除失败");
            return resultState;
        }
        resultState.setMsg("删除成功");
        return resultState;
    }
}
