package com.crm.manager.controller;

import com.crm.manager.service.UserEntryService;
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.UserEntrySearchListDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.user.info.UserEntryAddEntity;
import com.crm.rely.backend.core.entity.user.info.UserEntrySearchEntity;
import com.crm.rely.backend.core.entity.user.info.UserEntryUpdateEntity;
import com.crm.rely.backend.core.entity.user.info.UserEntryUpdateStatusEntity;
import com.crm.rely.backend.exception.ServiceException;
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/entry")
public class UserEntryController {

    @Autowired
    private UserEntryService userEntryService;

    /**
     * 添加入职人员
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated UserEntryAddEntity entity) throws Exception {
        if(!Strings.isNullOrEmpty(entity.getEmail()) &&  !Pattern.matches(RegexpConstants.REGEXP_EMAIL,entity.getEmail())){
            return BaseResultDto.error(Constants.EMAIL_FORMAT_ERROR);
        }
        if(Constants.MT4.equals(entity.getSalaryPlatform()) || Constants.MT5.equals(entity.getSalaryPlatform())){
           /* if (Strings.isNullOrEmpty(entity.getLogin())) {
                throw ServiceException.exception(Constants.LOGIN_NOT_NULL_ERROR);
            }*/
        }else{
            if (Strings.isNullOrEmpty(entity.getBankCode()) || Strings.isNullOrEmpty(entity.getBankName()) || Strings.isNullOrEmpty(entity.getAccountHoldersName()) || Strings.isNullOrEmpty(entity.getBankAccountNumber())) {
                throw ServiceException.exception("请完善银行信息");
            }
        }
        userEntryService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 入职人员填写信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated UserEntryUpdateEntity entity) throws Exception {
        userEntryService.update(entity);
        return BaseResultDto.success();
    }

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

        return userEntryService.searchPageList(entity);
    }

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

    @PostMapping("/approve/hr")
    public BaseResultDto updateHrAuditStatus(@RequestBody UserEntryUpdateStatusEntity entity) throws Exception {
        userEntryService.updateHrAuditStatus(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/approve/manage")
    public BaseResultDto updateStatus(@RequestBody UserEntryUpdateStatusEntity entity) throws Exception {
        userEntryService.updateStatus(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/add/info")
    public BaseResultDto getInfoFlag(@RequestBody UserEntryUpdateEntity entity) throws Exception {
        return BaseResultDto.success(Constants.SUCCESS,userEntryService.getInfoUseFlag(entity));
    }

    /**
     * 修改入职人员信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/update/manage")
    public BaseResultDto updateManage(@RequestBody @Validated UserEntryUpdateEntity entity) throws Exception {
        userEntryService.updateManage(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/add/googlemail")
    public BaseResultDto getGooglemail(@RequestBody UserEntryUpdateEntity entity) throws Exception {
        if (Strings.isNullOrEmpty(entity.getFirstName()) || Strings.isNullOrEmpty(entity.getLastName())) {
            return BaseResultDto.error(Constants.NAME_NOT_NULL);
        }
        return BaseResultDto.success(Constants.SUCCESS,userEntryService.getGooglemail(entity.getFirstName(),entity.getLastName(),entity.getEnglishName()));
    }

    /**
     * 身份认证获取web链接
     * @param entity
     * @return
     */
    @PostMapping("/add/websdklink")
    public BaseResultDto getWebsdkLink(@RequestBody UserEntryUpdateEntity entity) throws Exception{
        return userEntryService.getWebsdkLink(entity);
    }

}
