package com.xxfc.platform.vehicle.biz;

import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.vehicle.pojo.VehicleModelQueryCondition;
import com.xxfc.platform.vehicle.pojo.VehicleModelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.xxfc.platform.vehicle.entity.VehicleModel;
import com.xxfc.platform.vehicle.mapper.VehicleModelMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 车型
 *
 * @author zjw
 * @email nishijjo@qq.com
 * @date 2019-05-21 14:15:27
 */
@Service
@Slf4j
public class VehicleModelBiz extends BaseBiz<VehicleModelMapper, VehicleModel> {


    //添加评分
    public void addScore(Integer id, Integer score) {
        VehicleModel model = new VehicleModel();
        model.setId(id);
        model.setScore(score);
        updateSelectiveById(model);
    }

    public ObjectRestResponse findVehicleModelPage(VehicleModelQueryCondition vmqc) {

//        List<VehicleModelVo > vehicleModelPage = mapper.findVehicleModelPage(vmqc);

//    log.debug("vehicleModelPage"+vehicleModelPage);


        try {
            PageDataVO<VehicleModelVo> mPageDataVO = PageDataVO.pageInfo(vmqc.getPage(),
                    vmqc.getLimit(), () -> mapper.findVehicleModelPage(vmqc));

//            List<VehicleModelVo> vehicleModelVos = mPageDataVO.getData();
//            vehicleModelVos.parallelStream().forEach(vehicleModelVo -> vehicleModelVo.setPicture(vehicleModelVo.getPicture().split(",")[0]));

            return ObjectRestResponse.succ(mPageDataVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ObjectRestResponse.createDefaultFail();

    }

    @Transactional
    public Integer insertAndGetId(VehicleModel vm) {
        try {
            mapper.insertSelective(vm);
            return vm.getId();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return null;

    }

    /**
     *更新车型的评分
     * @param modelId
     * @param avgScore
     * @return
     */
    public int updateScoreByModelIdAndavgScore(Integer modelId, int avgScore) {
      return  mapper.updateScoreByModelIdAndavgScore(modelId,avgScore);
    }


    public void updateByPrimaryKeySelective(VehicleModel vm){
        mapper.updateByPrimaryKeySelective(vm);
    }
}