package com.sqx.modules.resumes.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.entity.CompanyUser;
import com.sqx.modules.resumes.entity.PostPush;
import com.sqx.modules.resumes.service.CompanyService;
import com.sqx.modules.resumes.service.CompanyUserService;
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.util.HashMap;

@RestController
@Api(value = "企业", tags = {"企业"})
@RequestMapping(value = "/app/company")
public class AppCompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyUserService companyUserService;

    @Login
    @PostMapping("/insertCompany")
    @ApiOperation("添加企业")
    public Result insertCompany(@RequestBody Company company,@RequestAttribute Long userId){
        company.setUserId(userId);
        return companyService.insertCompany(company,userId);
    }

    @Login
    @PostMapping("/updateCompany")
    @ApiOperation("修改企业")
    public Result updateCompany(@RequestBody Company company){
        company.setStatus(1);
        return companyService.updateCompany(company);
    }

//    @Login
//    @GetMapping("/selectCompanyByUserId")
//    @ApiOperation("查询用户的企业")
//    public Result selectCompanyByUserId(@RequestAttribute  Long userId){
//        return Result.success().put("data",companyService.selectCompanyByUserId(userId));
//    }
    @Login
    @GetMapping("selectCompanyByUserId")
    @ApiOperation("是否是公司员工")
    public Result isCompanyUser(@RequestAttribute("userId")Long userId){
        CompanyUser companyUser = companyUserService.getOne(new QueryWrapper<CompanyUser>().eq("user_id",userId).eq("is_use",1));
        if (companyUser == null){
        QueryWrapper<Company> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).orderByDesc("update_time").last("limit 1 ");
        Company company = companyService.getBaseMapper().selectOne(queryWrapper);
        if (company==null){
            return Result.error("未进入公司");
        }
            if(company.getStatus()==1){
            company.setMessage("审核中");
            return Result.success().put("data",company);
        }else{
                company.setMessage(company.getAuditContent());
                return Result.success().put("data",company);
            }
        }
        else {
            Company company = companyService.getById(companyUser.getCompanyId());
            company.setMessage("审核通过");
            return Result.success().put("data",company);
        }
    }
    @GetMapping("/selectCompanyByUserIds")
    @ApiOperation("查询用户的企业")
    public Result selectCompanyByUserIds(Long userId){
        return Result.success().put("data",companyService.selectCompanyByUserId(userId));
    }

    @GetMapping("/selectCompanyList")
    @ApiOperation("查询企业列表")
    public Result selectCompanyList(Integer page,Integer limit,String startTime,String endTime, String companyName,String companyPhone,String userName,Integer status){
        return companyService.selectCompanyList(page, limit, startTime, endTime, companyName, companyPhone, userName, status);
    }

    @GetMapping("/selectCompanyByCompanyId")
    @ApiOperation("查询企业详情")
    public Result selectCompanyByCompanyId(Long companyId){
        return Result.success().put("data",companyService.selectCompanyByCompanyId( companyId));
    }

    @Login
    @PostMapping("/deleteCompany")
    @ApiOperation("删除企业")
    public Result deleteCompany(Long companyId){

        return Result.success().put("data",companyService.removeById(companyId));
    }

    @Login
    @GetMapping("/selectCompanyListByMember")
    @ApiOperation("查询合作企业")
    public Result selectCompanyListByMember(Integer page,Integer limit){
        return companyService.selectCompanyListByMember(page, limit);
    }

    @Login
    @PostMapping("/giveCompany")
    @ApiOperation("转让企业")
    public Result giveCompany(String phone,String msg,String givePhone){
        return companyService.giveCompany(phone, msg, givePhone);
    }

    @GetMapping("/listCompany")
    @ApiOperation("企业列表(不分页)")
    public Result listCompany(Integer page, Integer limit, String companyName, String city, String companyScope, String companyPeople){
        return companyService.listCompany(page, limit, companyName, city, companyScope, companyPeople);
    }
    @GetMapping("/getCompanyPostPush")
    @ApiOperation("获取公司岗位列表")
    public Result getCompanyPostPush(String key, Integer page, Integer limit,Double lng,Double lat, PostPush postPush){
        return companyService.selectCompanyPostPushList(key,postPush,page,limit,lat,lng);
    }
}
