package com.xxfc.platform.vehicle.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.vo.GoodDataVO;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.vehicle.entity.Vehicle;
import com.xxfc.platform.vehicle.pojo.VehicleModelQueryCondition;
import com.xxfc.platform.vehicle.pojo.VehicleModelVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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 tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.ArrayList;
import java.util.Arrays;
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);
    }

    /**
     * 分页查询车型列表
     * @param vmqc
     * @return
     */
    public ObjectRestResponse findVehicleModelPage(VehicleModelQueryCondition vmqc) {

        try {
            PageDataVO<VehicleModelVo> mPageDataVO = PageDataVO.pageInfo(vmqc.getPage(),
                    vmqc.getLimit(), () -> mapper.findVehicleModelPage(vmqc));
            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);
    }

    /**
     * 分页查询返回List<GoodDataVO>
     * @param page
     * @param limit
     * @return
     */
    public List<GoodDataVO> goodList(Integer page, Integer limit) {
        List<GoodDataVO> goodDataVOS = new ArrayList<>();

        /*设置分页*/
        Page<VehicleModel> pages = PageHelper.startPage(page, limit);

        Example example = Example.builder(VehicleModel.class)
                .where(
                        WeekendSqls.<VehicleModel>custom()
                                .andEqualTo(VehicleModel::getIsdel, 0)
                                .andEqualTo(VehicleModel::getHotSign,1)
                ).orderByDesc("id").build();
        List<VehicleModel> vehicleModels = selectByExample(example);
        if (CollectionUtils.isEmpty(vehicleModels)) {
            return null;
        }

        PageInfo<VehicleModel> pageInfo= PageInfo.of(vehicleModels);
        List<VehicleModel> vehicleModelList = pageInfo.getList();
        /*遍历车型列表设置List<GoodDataVO>*/
        vehicleModelList.forEach(vm ->{
            GoodDataVO goodDataVO = new GoodDataVO();
            goodDataVO.setId(vm.getId());
            goodDataVO.setImgUrl(vm.getPicture());
            goodDataVO.setName(vm.getName());
            goodDataVO.setName1(vm.getKeyword());
            goodDataVO.setPrice(String.valueOf(vm.getBuyPrice()));
            System.out.println("++++++++++++++++++++++++++++++++++++++"+goodDataVO+"++++++++++++++++++++++++++++++++++++++");
            goodDataVOS.add(goodDataVO);});
//        for (VehicleModel vm : vehicleModelList) {
//
//        }
        return goodDataVOS;
    }
}