package cn.com.ty.lift.ud.schoolVideo.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.userCollect.service.IUserCollectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.service.IService;
import com.mysql.cj.util.StringUtils;

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.dataBank.mapper.entity.LiftBrandEntity;
import cn.com.ty.lift.ud.dataBank.service.ILiftBrandService;
import cn.com.ty.lift.ud.schoolVideo.controller.query.SchoolVideoQuery;
import cn.com.ty.lift.ud.schoolVideo.mapper.entity.SchoolVideoEntity;
import cn.com.ty.lift.ud.schoolVideo.mapper.entity.SchoolVideoOperateEntity;
import cn.com.ty.lift.ud.schoolVideo.service.ISchoolVideoOperateService;
import cn.com.ty.lift.ud.schoolVideo.service.ISchoolVideoService;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 电梯学堂 API
 * 
 * @author Xson
 * @date 2020-04-23
 */
@Api(tags = "电梯学堂 API- 魏瑞锋")
@RestController
@RequestMapping("/api/schoolVideos")
public class SchoolVideoController extends BaseController<SchoolVideoEntity, SchoolVideoQuery> {

    private final Logger log = LoggerFactory.getLogger(SchoolVideoController.class);
    @Autowired
    private ISchoolVideoService schoolVideoService;
    @Autowired
    private ILiftBrandService liftBrandService;
    @Autowired
    private ISchoolVideoOperateService schoolVideoOperateService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserCollectService iUserCollectService;

    @Override
    protected IService<SchoolVideoEntity> getService() {
        return schoolVideoService;
    }

  //新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    @Override
    public RestResponse<SchoolVideoEntity> create(@RequestBody SchoolVideoEntity entity) {
        return  schoolVideoService.add(entity);
    }
    
    @ApiOperation(value = "批量上架")
    @PostMapping("useBath")
    public RestResponse useBath(@RequestBody DelReq ids) {
        schoolVideoService.updateByIds(ids.getIds(), 1);
        return RestResponse.success( "上架成功");
    }

    @ApiOperation(value = "批量下架")
    @PostMapping("noUseBath")
    public RestResponse noUseBath(@RequestBody DelReq ids) {
        schoolVideoService.updateByIds(ids.getIds(), 0);
        return RestResponse.success( "下架成功");
    }

    @ApiOperation(value = "批量首页显示")
    @PostMapping("showIndex")
    public RestResponse showIndex(@RequestBody DelReq ids) {
        schoolVideoService.updateHotFlagByIds(ids.getIds(), 1);
        return RestResponse.success( "首页显示");
    }

    @ApiOperation(value = "取消批量首页显示")
    @PostMapping("noshowIndex")
    public RestResponse noshowIndex(@RequestBody DelReq ids) {
        schoolVideoService.updateHotFlagByIds(ids.getIds(), 0);
        return RestResponse.success( "取消首页显示");
    }

    @ApiOperation(value = "审批 1:通过 0:驳回")
    @PostMapping("/examine")
    public RestResponse examine(@RequestBody SchoolVideoEntity neDto) {
        return schoolVideoService.examineById(neDto);
    }

    @PostMapping("/query-page")
    @ApiOperation(value = "分页查询")
    @Override
    public RestResponse getCondition(@RequestBody SchoolVideoQuery request) {
        if(StringUtils.isNullOrEmpty(request.getPlatform())  ){
            return RestResponse.fail("平台不能为空");
        }
        return schoolVideoService.selectByExample(request);
    }

    // 根据id查详情
    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    @Override
    public RestResponse get(@PathVariable Long id) {
        SchoolVideoEntity entity = getService().getById(id);
        LiftBrandEntity byId = liftBrandService.getById(entity.getBrandId());
        UserInfoEntity user = userInfoService.getByUserId(entity.getUserId());
        if (user != null) {
            entity.setAvatarUrl(user.getAvatarUrl());
            entity.setNickname(user.getName());
        }
        entity.setBrandName(byId.getName());
        return RestResponse.success(entity, "Queried successfully");
    }

    @GetMapping("/detailWithRecord")
    @ApiOperation(value = "获取详情，app端")
    public RestResponse detailWithRecord(Long id, Long userId) {
        SchoolVideoEntity entity = getService().getById(id);
        schoolVideoService.updateBrowseNum(id);
        LiftBrandEntity byId = liftBrandService.getById(entity.getBrandId());
        UserInfoEntity user = userInfoService.getByUserId(entity.getUserId());
        if (user != null) {
            entity.setAvatarUrl(user.getAvatarUrl());
            entity.setNickname(user.getName());
        }
        if (byId != null) {
            entity.setBrandName(byId.getName());
        }
        List<SchoolVideoOperateEntity> findByUserAndId = schoolVideoOperateService.findByUserAndId(userId, id, 2);
        if (!findByUserAndId.isEmpty()) {
            entity.setLikeId(findByUserAndId.get(0).getId());
            entity.setIsLike(1);
        } else {
            entity.setIsLike(0);
        }
        List<SchoolVideoOperateEntity> findByUserAndId2 = schoolVideoOperateService.findByUserAndId(userId, id, 3);
        if (!findByUserAndId2.isEmpty()) {
            entity.setFavoriteId(findByUserAndId2.get(0).getId());
            entity.setIsFavorite(1);
        } else {
            entity.setIsFavorite(0);
        }
        List<SchoolVideoOperateEntity> findByUserAndId3 = schoolVideoOperateService.findByUserAndId(userId, id, 4);
        if(!findByUserAndId3.isEmpty()) {
            entity.setIsExceptional(1);
        }else {
            entity.setIsExceptional(0);
        }
        return RestResponse.success(entity, "Queried successfully");
    }

    /**
     * 重写根据id批量删除的方法
     * @param ids
     * @return
     */
    @DeleteMapping("/delete-batch")
    @ApiOperation(value = "批量删除")
    @Override
    public RestResponse deleteBath(@RequestBody DelReq req) {
        boolean deleteFlag = getService().removeByIds(req.getIds());
        if (deleteFlag) {
            for(Long id : req.getIds()){
                //删除 user_collect 我的收藏的内容
                iUserCollectService.deleteByTypeAndDataId(id,2);
            }
            return RestResponse.success(null, "删除成功");
        }else {
            return RestResponse.fail("删除失败");
        }
    }

    /**
     * 重写根据id删除的方法
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    @Override
    public RestResponse delete(@RequestBody DelReq req) {
        boolean deleteFlag = getService().removeById(req.getId());
        if (deleteFlag) {
            //删除 user_collect 我的收藏的内容
            iUserCollectService.deleteByTypeAndDataId(req.getId(),2);
            return RestResponse.success(null, "删除成功");
        }else {
            return RestResponse.fail("删除失败");
        }
    }

    /**
     * 重写修改的方法
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody SchoolVideoEntity request) {
        SchoolVideoEntity byId = schoolVideoService.getById(request.getId());
        if (byId.getCheckFlag() == 0) {
            request.setCheckFlag(2);
        }
        boolean updateFlag = getService().updateById(request);
        if (updateFlag) {
            return RestResponse.success(null, "修改成功");
        }else {
            return RestResponse.fail("修改失败");
        }
    }
}