package com.crm.manager.controller;

import com.crm.manager.service.UserHireService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.RegexpConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.user.info.UserHireSearchListDto;
import com.crm.rely.backend.core.dto.user.info.UserReferralDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.user.info.*;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 java.util.regex.Pattern;

@RestController
@RequestMapping("/user/hire")
public class UserHireController {

    @Autowired
    private UserHireService userHireService;

    /**
     * 获取招聘推广链接
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/getUrl")
    public BaseResultDto getUrl(@RequestBody UserHireUpdateEntity entity) throws Exception {
        if(null == entity.getId() && Strings.isNullOrEmpty(entity.getReferralCode())){
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        return BaseResultDto.success(Constants.SUCCESS,userHireService.getUrl(entity));
    }

    /**
     * 添加招聘人员
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated UserHireAddEntity entity) throws Exception {
        if(Strings.isNullOrEmpty(entity.getEmail()) && Strings.isNullOrEmpty(entity.getPhoneNumbe()) && Strings.isNullOrEmpty(entity.getHireFile())){
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        if(!Strings.isNullOrEmpty(entity.getEmail()) &&  !Pattern.matches(RegexpConstants.REGEXP_EMAIL,entity.getEmail())){
            return BaseResultDto.error(Constants.EMAIL_FORMAT_ERROR);
        }

        userHireService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改招聘人员
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated UserHireUpdateEntity entity) throws Exception {
        userHireService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<UserHireSearchListDto> pageList(@RequestBody UserHireSearchEntity entity) throws Exception {

        return userHireService.searchPageList(entity);
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        UserHireSearchListDto result = userHireService.searchSingle(entity);
        return BaseResultDto.success(result);
    }

    /**
     * 修改招聘人员状态
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update/status")
    public BaseResultDto updateStatus(@RequestBody UserHireUpdateEntity entity) throws Exception {
        userHireService.updateStatus(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/add/referral/list")
    public ResultWithPagerDto<UserReferralDto> getReferralList(@RequestBody UserReferralEntity entity) throws Exception {
        return userHireService.getReferralList(entity);
    }

}
