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

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

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.RecruitmentInfoQuery;
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.recruitmentInfos.mapper.entity.RecruitmentDeliveryEntity;
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.IRecruitmentDeliveryService;
import cn.com.ty.lift.ud.recruitmentInfos.service.IRecruitmentInfoService;
import cn.com.ty.lift.ud.recruitmentInfos.service.IRecruitmentOperateService;
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/recruitmentInfos")
public class RecruitmentInfoController extends BaseController<RecruitmentInfoEntity, RecruitmentInfoQuery> {

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

    private @Autowired IRecruitmentInfoService recruitmentInfoService;
    private @Autowired IRecruitmentOperateService recruitmentOperateService;
    private @Autowired IRecruitmentDeliveryService recruitmentDeliveryService;

    @Override
    protected IService<RecruitmentInfoEntity> getService() {
        return recruitmentInfoService;
    }

    private @Autowired
    IUserCollectService iUserCollectService;

    //新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    @Override
    public RestResponse<RecruitmentInfoEntity> create(@RequestBody RecruitmentInfoEntity entity) {
        entity.setBrowseNum(0);
        entity.setFavoriteNum(0);
        entity.setLikeNum(0);
        entity.setStatuz(0);
        entity.setHotFlag(0);
        boolean saveFlag = getService().save(entity);
        if (saveFlag)
            return RestResponse.success(null, "Created successfully");
        else
            return RestResponse.fail("Created unsuccessfully");
    }

    @ApiOperation(value = "批量上架")
    @PostMapping("useBath")
    public RestResponse useBath(@RequestBody DelReq req) {
        recruitmentInfoService.updateStatuzByIds(req.getIds(), 1);
        return RestResponse.success(null, "上架成功");
    }

    @ApiOperation(value = "批量下架")
    @PostMapping("nouseBath")
    public RestResponse nouseBath(@RequestBody DelReq req) {
        recruitmentInfoService.updateStatuzByIds(req.getIds(), 0);
        return RestResponse.success(null, "下架成功");
    }

    @ApiOperation(value = "批量首页展示")
    @PostMapping("showBath")
    public RestResponse showBath(@RequestBody DelReq req) {
        recruitmentInfoService.updateHotFlagByIds(req.getIds(), 1);
        return RestResponse.success(null, "请求成功");
    }

    @ApiOperation(value = "批量取消首页展示")
    @PostMapping("noshowBath")
    public RestResponse noshowBath(@RequestBody DelReq req) {
        recruitmentInfoService.updateHotFlagByIds(req.getIds(), 0);
        return RestResponse.success(null, "请求成功");
    }

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

    @GetMapping("/query/{id}")
    @ApiOperation(value = "获取详情")
    @Override
    public RestResponse get(@PathVariable("id") Long id) {
        RecruitmentInfoEntity finOneById = recruitmentInfoService.finOneById(id);
        return RestResponse.success(finOneById, "Queried successfully");
    }

    @GetMapping("detailWithRecord")
    @ApiOperation(value = "获取详情，app端")
    public RestResponse detailWithRecord(Long id, Long userId) {
        RecruitmentInfoEntity entity = recruitmentInfoService.finOneById(id);
        recruitmentInfoService.updateBrowseNum(id);
        RecruitmentOperateEntity findByUserAndId = recruitmentOperateService.findByUserAndId(userId, id, 3);
        RecruitmentDeliveryEntity findByDataIdAndResumeId = recruitmentDeliveryService.findByDataIdAndResumeId(id, userId);
        if (findByDataIdAndResumeId != null) {
            entity.setIsApply(1);
        } else {
            entity.setIsApply(0);
        }
        if (findByUserAndId != null) {
            entity.setFavoriteId(findByUserAndId.getId());
            entity.setIsFavorite(1);
        } else {
            entity.setIsFavorite(0);
        }
        return RestResponse.success(entity, "Queried successfully");
    }

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

    /**
     * 重写根据id批量删除的方法
     * @param req
     * @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,3);
            }
            return RestResponse.success(null, "删除成功");
        }else {
            return RestResponse.fail("删除失败");
        }
    }

    /**
     * 重写根据id删除的方法
     * @param req
     * @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(),3);
            return RestResponse.success(null, "删除成功");
        }else {
            return RestResponse.fail("删除失败");
        }
    }

    /**
     * 重写修改的方法
     * @param request
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    @Override
    public RestResponse update(@RequestBody RecruitmentInfoEntity request) {
        RecruitmentInfoEntity byId = recruitmentInfoService.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("修改失败");
        }
    }
}