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

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.schoolVideo.controller.query.SchoolVideoOperateQuery;
import cn.com.ty.lift.ud.schoolVideo.mapper.entity.SchoolVideoEntity;
import cn.com.ty.lift.ud.userCollect.mapper.entity.UserCollectDto;
import cn.com.ty.lift.ud.userCollect.mapper.entity.UserCollectEntity;
import cn.com.ty.lift.ud.userCollect.service.IUserCollectService;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import com.baomidou.mybatisplus.extension.service.IService;

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.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/schoolVideoOperates")
public class SchoolVideoOperateController extends BaseController<SchoolVideoOperateEntity, SchoolVideoOperateQuery> {

    private final Logger log = LoggerFactory.getLogger(SchoolVideoOperateController.class);

    private @Autowired ISchoolVideoOperateService schoolVideoOperateService;
    private @Autowired IUserCollectService iUserCollectService;
    private @Autowired ISchoolVideoService schoolVideoService;

    @Override
    protected IService<SchoolVideoOperateEntity> getService() {
        return schoolVideoOperateService;
    }

    @Override
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<SchoolVideoOperateEntity> create(@RequestBody SchoolVideoOperateEntity entity) {
        // 保存用户收藏记录
        UserCollectDto userCollect = new UserCollectDto();
        userCollect.setUserId(entity.getUserId());
        userCollect.setCollectType(2);
        userCollect.setInfoId(entity.getDataId());
        UserCollectEntity collect = iUserCollectService.isCollect(userCollect);
        if (null == collect) {
            UserCollectEntity userCollectEntity = new UserCollectEntity();
            BeanUtils.copyProperties(userCollect, userCollectEntity);
            iUserCollectService.save(userCollectEntity);
        }
        List<SchoolVideoOperateEntity> findByUserAndId =
            schoolVideoOperateService.findByUserAndId(entity.getUserId(), entity.getDataId(), entity.getType());
        boolean saveFlag = false;
        if (findByUserAndId.isEmpty()) {
            saveFlag = schoolVideoOperateService.save(entity);
        }
        SchoolVideoOperateEntity byId = schoolVideoOperateService.getById(entity.getId());
        if (saveFlag) {
            if (entity.getType() == 3) {
                SchoolVideoEntity byId2 = schoolVideoService.getById(entity.getDataId());
                byId2.setFavoriteNum(byId2.getFavoriteNum() == null ? 1 : byId2.getFavoriteNum() + 1);
                schoolVideoService.updateById(byId2);
            }
            if (entity.getType() == 2) {
                SchoolVideoEntity byId2 = schoolVideoService.getById(entity.getDataId());
                byId2.setLikeNum(byId2.getLikeNum() == null ? 1 : byId2.getLikeNum() + 1);
                schoolVideoService.updateById(byId2);
            }
            return RestResponse.success(null, "Created successfully");
        } else {
            return RestResponse.fail("Created unsuccessfully");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除")
    public RestResponse delete(@RequestBody DelReq req) {
        SchoolVideoOperateEntity byId2 = schoolVideoOperateService.getById(req.getId());
        SchoolVideoEntity byId = schoolVideoService.getById(byId2.getDataId());
        if (byId2.getType() == 3) {
            byId.setFavoriteNum(byId.getFavoriteNum() == null ? 0 : byId.getFavoriteNum() - 1);
            schoolVideoService.updateById(byId);
        }
        if (byId2.getType() == 2) {
            byId.setLikeNum(byId.getLikeNum() == null ? 0 : byId.getLikeNum() - 1);
            schoolVideoService.updateById(byId);
        }
        iUserCollectService.deleteByUserAndDataId(byId2.getUserId(), byId2.getDataId(), 2);
        boolean deleteFlag = getService().removeById(req.getId());
        if (deleteFlag) {
            return RestResponse.success(null, "Deleted successfully");
        } else {
            return RestResponse.fail("Deleted unsuccessfully");
        }
    }
}