package com.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.entity.BrandEntity;
import com.platform.entity.RecommendEntity;
import com.platform.entity.Result;
import com.platform.service.BrandService;
import com.platform.service.RecommendService;
import com.platform.utils.CommonDataUtils;

import com.platform.vo.RecommendEntityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * Created by Hb_zzZ on 2018/12/7.
 */
@Slf4j
@Controller
@RequestMapping("/recommend")
public class RecommendController extends AbstractController{

    @Autowired
    private RecommendService service;

    @Autowired
    private BrandService brandService;

    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody RecommendEntity bo){
        Result dto;
        if(bo == null || StringUtils.isEmpty(bo.getTitle()) || StringUtils.isEmpty(bo.getImage())
                || bo.getConnectId() == null || bo.getOrderNum() == null){
            dto = Result.failure("参数不能为空");
            log.info("参数不能为空");
            return dto;
        }
        bo.setCreateUserId(getUserId());
        bo.setUpdateUserId(getUserId());
        int n = service.save(bo);
        if(n <= 0){
            dto = Result.failure("新增推荐位失败");
            log.info("新增推荐位失败");
            return dto;
        }

        dto = Result.success("新增推荐位成功",null);
        return dto;
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(@RequestBody RecommendEntity bo){
        if(bo == null || bo.getId() == null){
            log.info("参数不能为空");
            return Result.failure("参数不能为空");
        }
        RecommendEntity entity = service.selectById(bo.getId());
        RecommendEntityVo vo = new RecommendEntityVo();
        BeanUtils.copyProperties(entity,vo);
        BrandEntity sceneEntity = brandService.queryObject(entity.getConnectId().intValue());
        if(!ObjectUtils.isEmpty(sceneEntity)){
            vo.setSceneName(sceneEntity.getName());
            vo.setSceneUrl(sceneEntity.getLiveRoomUrl());
        }
        return Result.success( vo);
    }

    @PostMapping("/selectByType")
    @ResponseBody
    public Result selectByType(@RequestBody RecommendEntityVo bo){
        if(ObjectUtils.isEmpty(bo.getPageNum())){
            bo.setPageNum(1L);
        }
        if(ObjectUtils.isEmpty(bo.getLimit())){
            bo.setLimit(10L);
        }
        //管理后台根据type返回相应数据
        IPage<RecommendEntity> resultPage = service.selectByType(bo);
        List<RecommendEntityVo> entityVos = resultPage.getRecords().parallelStream().map(entity -> {
            RecommendEntityVo vo = new RecommendEntityVo();
            BeanUtils.copyProperties(entity,vo);
            BrandEntity sceneEntity = brandService.queryObject(entity.getConnectId().intValue());
            if(!ObjectUtils.isEmpty(sceneEntity)){
                vo.setSceneName(sceneEntity.getName());
                vo.setSceneUrl(sceneEntity.getLiveRoomUrl());
            }
            return vo;
        }).collect(Collectors.toList());
        return Result.success(CommonDataUtils.assembleResult(resultPage.getTotal() , resultPage.getPages() ,
                resultPage.getCurrent() , entityVos));
    }

    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody RecommendEntity bo){
        Result dto;
        if(bo == null || bo.getId() == null  || StringUtils.isEmpty(bo.getTitle())
                || StringUtils.isEmpty(bo.getImage()) || bo.getConnectId() == null
                || bo.getOrderNum() == null){
            dto = Result.failure("参数不能为空");
            log.info("参数不能为空");
            return dto;
        }
        bo.setUpdateUserId(getUserId());
        int n = service.update(bo);
        if(n <= 0){
            dto = Result.failure("修改推荐位失败");
            log.info("修改推荐位失败");
            return dto;
        }

        dto = Result.success("修改推荐位成功",null);
        return dto;
    }

    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody RecommendEntity bo){
        Result dto;
        if(bo == null || bo.getId() == null){
            dto = Result.failure("参数不能为空");
            log.info("参数不能为空");
            return dto;
        }
        bo.setUpdateUserId(getUserId());
        int n = service.delete(bo.getId());
        if(n <= 0){
            dto = Result.failure("删除推荐位失败");
            log.info("删除推荐位失败");
            return dto;
        }

        dto = Result.success("删除推荐位成功",null);
        return dto;
    }
}
