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

import cn.com.ty.lift.ud.common.BaseController;
import cn.com.ty.lift.ud.common.DelReq;
import cn.com.ty.lift.ud.recruitmentInfos.controller.query.RecruitmentOperateQuery;
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.recruitmentInfos.mapper.entity.RecruitmentInfoEntity;
import cn.com.ty.lift.ud.recruitmentInfos.mapper.entity.RecruitmentOperateEntity;
import cn.com.ty.lift.ud.recruitmentInfos.service.IRecruitmentInfoService;
import cn.com.ty.lift.ud.recruitmentInfos.service.IRecruitmentOperateService;
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 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/recruitmentOperates")
public class RecruitmentOperateController extends BaseController<RecruitmentOperateEntity, RecruitmentOperateQuery> {

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

    private @Autowired IRecruitmentOperateService recruitmentOperateService;
    private @Autowired IRecruitmentInfoService recruitmentInfoService;
    @Autowired
    private IUserCollectService iUserCollectService;

    @Override
    protected IService<RecruitmentOperateEntity> getService() {
        return recruitmentOperateService;
    }

    @Override
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    public RestResponse<RecruitmentOperateEntity> create(@RequestBody RecruitmentOperateEntity entity) {
        // 保存用户收藏记录
        UserCollectDto userCollect = new UserCollectDto();
        userCollect.setUserId(entity.getUserId());
        userCollect.setCollectType(3);
        userCollect.setInfoId(entity.getDataId());
        UserCollectEntity collect = iUserCollectService.isCollect(userCollect);
        if (null == collect) {
            UserCollectEntity userCollectEntity = new UserCollectEntity();
            BeanUtils.copyProperties(userCollect, userCollectEntity);
            iUserCollectService.save(userCollectEntity);
        }
        RecruitmentOperateEntity findByUserAndId =
            recruitmentOperateService.findByUserAndId(entity.getUserId(), entity.getDataId(), entity.getType());
        boolean saveFlag = false;
        if (findByUserAndId == null) {
            saveFlag = recruitmentOperateService.save(entity);
        }
        RecruitmentOperateEntity byId = recruitmentOperateService.getById(entity.getId());
        if (saveFlag) {
            if (entity.getType() == 3) {
                RecruitmentInfoEntity byId2 = recruitmentInfoService.getById(entity.getDataId());
                byId2.setFavoriteNum(byId2.getFavoriteNum() == null ? 1 : byId2.getFavoriteNum() + 1);
                recruitmentInfoService.updateById(byId2);
            }
            return RestResponse.success(null, "收藏成功");
        } else {
            return RestResponse.fail("收藏失败");
        }
    }

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