package com.sqx.modules.intention.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.intention.entity.Intention;
import com.sqx.modules.intention.service.IntentionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author www.javacoder.top
 * @since 2023-01-13
 */
@Api(value = "求职期望", tags = {"求职期望"})

@RestController
@RequestMapping("/app/intention/")
public class AppIntentionController {
    @Autowired
    private IntentionService intentionService;

    @Login
    @ApiOperation("添加求职意向")
    @PostMapping(value = "saveUpdate")
    public Result saveUpdate(@RequestAttribute("userId") Long userId, @RequestBody Intention Intention) {
        Intention.setUserId(userId);
        Intention.setClassifyOneId(0L);
        Intention.setClassifyTwoId(0L);
        Intention.setCreateTime(LocalDateTime.now());
        Intention.setIndustryId(201L);
        Intention.setIndustryOneId(200L);
        Intention.setIsDefault(1);
        String[] parts = Intention.getSalaryRange().trim().split("-", 2);
        Intention.setMinSalary(Integer.parseInt(parts[0].trim()));
        Intention.setMaxSalary(Integer.parseInt(parts[1].trim()));
        return intentionService.saveUpdate(Intention);
    }

    @Login
    @ApiOperation("获取求职意向列表")
    @GetMapping(value = "getIntentionList")
    public Result getIntentionList(@RequestAttribute("userId") Long userId) {
        QueryWrapper<Intention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("is_default",1);
        return Result.success().put("data", intentionService.getBaseMapper().selectList(queryWrapper));
    }

    @Login
    @ApiOperation("获取求职意向信息")
    @GetMapping(value = "getIntentionInfo")
    public Result getIntentionInfo(Long intentionId) {
        QueryWrapper<Intention> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("intention_id",intentionId);
        return Result.success().put("data", intentionService.getOne(queryWrapper));


    }

    @Login
    @ApiOperation("删除求职意向")
    @PostMapping(value = "deleteIntention")
    public Result deleteIntention(@RequestBody Intention intention) {
        return intentionService.removeById(intention.getIntentionId()) ? Result.success() : Result.error();
    }
}

