package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.UserHireMapper;
import com.crm.manager.dao.mapper.UserReferralMapper;
import com.crm.manager.dao.repository.UserHireRepository;
import com.crm.manager.dao.repository.UserInfoRepository;
import com.crm.manager.dao.repository.UserReferralRepository;
import com.crm.manager.service.SysConfigService;
import com.crm.manager.service.UserEntryService;
import com.crm.manager.service.UserHireService;
import com.crm.rely.backend.core.constant.ConfigConstants;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.PageDto;
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.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.core.pojo.table.UserHireTable;
import com.crm.rely.backend.core.pojo.table.UserInfoTable;
import com.crm.rely.backend.core.pojo.table.UserReferralTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.GetRandom;
import com.google.common.base.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserHireServiceImpl implements UserHireService {
    private static String randUpperCaseString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    @Autowired
    private UserHireRepository userHireRepository;
    @Autowired
    private UserReferralRepository userReferralRepository;
    @Autowired
    private UserEntryService userEntryService;
    @Autowired
    private UserHireMapper userHireMapper;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private UserReferralMapper userReferralMapper;

    @Override
    public String getUrl(UserHireUpdateEntity entity) throws Exception {
        SysConfigTable sysConfigTable = sysConfigService.getByCode(ConfigConstants.JOB_PROMOTION_LINK);
        if (sysConfigTable == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }

        if(Strings.isNullOrEmpty(entity.getReferralCode())){
            UserReferralTable userReferralTable = userReferralRepository.getFirstByReferralUserId(entity.getId());
            if (null == userReferralTable) {
                UserInfoTable userTable = userInfoRepository.getFirstById(entity.getId());
                userReferralTable = new UserReferralTable();
                userReferralTable.setReferralUserId(userTable.getId());
                userReferralTable.setReferralName(userTable.getUsername());
                userReferralTable.setEmail(userTable.getEmail());
                userReferralTable.setAddTime(new Date());
                userReferralTable.setReferralNum(0);
                userReferralTable.setReferralSuccessNum(0);
                userReferralTable.setReferralCode(GetRandom.getRandom(randUpperCaseString,6));
                userReferralRepository.save(userReferralTable);
            }
            entity.setReferralCode(userReferralTable.getReferralCode());
        }

        return sysConfigTable.getValue() + "/" + entity.getReferralCode();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserHireAddEntity entity) throws ServiceException {
        UserHireTable table = new UserHireTable();
        BeanUtils.copyProperties(entity, table);
        table.setHireStatus(1);
        userHireRepository.save(table);

        if (!Strings.isNullOrEmpty(table.getReferralCode())) {
            UserReferralTable userReferralTable = userReferralRepository.findFirstByReferralCode(table.getReferralCode());
            if (null == userReferralTable) {
                throw ServiceException.exception(Constants.REFERRAL_ERROR);
            }
            userReferralTable.setReferralNum(userReferralTable.getReferralNum() + 1);
            userReferralRepository.save(userReferralTable);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserHireUpdateEntity entity) throws ServiceException {
        UserHireTable table = userHireRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        userHireRepository.save(table);
    }

    @Override
    public UserHireSearchListDto searchSingle(SingleLongEntity entity) throws ServiceException {
        UserHireTable table = userHireRepository.getFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        UserHireSearchListDto userHireSearchListDto = transformSearchListDto(table);
        return userHireSearchListDto;
    }


    @Override
    public ResultWithPagerDto<UserHireSearchListDto> searchPageList(UserHireSearchEntity entity) throws ServiceException {
        Integer count = userHireMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<UserHireTable> userHireTables = userHireMapper.pageList(entity);

        if (userHireTables == null || userHireTables.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<UserHireSearchListDto> userHireSearchListDtos = new ArrayList<>(userHireTables.size());

        for (UserHireTable userHireTable : userHireTables) {
            UserHireSearchListDto userHireSearchListDto = transformSearchListDto(userHireTable);
            userHireSearchListDtos.add(userHireSearchListDto);
        }

        return ResultWithPagerDto.success(pageDto, userHireSearchListDtos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(UserHireUpdateEntity entity) throws Exception {
        UserHireTable table = userHireRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        if (3 == table.getHireStatus() || 4 == table.getHireStatus()) {
            throw ServiceException.exception("招聘结果不能修改");
        }
        table.setHireStatus(entity.getHireStatus());
        table.setHireDesc(entity.getHireDesc());
        userHireRepository.save(table);

        if (3 == entity.getHireStatus()) {
            UserEntryAddEntity entryEntity = new UserEntryAddEntity();
            entryEntity.setHareId(table.getId());
            entryEntity.setEntryName(table.getHireName());
            entryEntity.setFirstName(table.getFirstName());
            entryEntity.setLastName(table.getLastName());
            entryEntity.setEmail(table.getEmail());
            entryEntity.setReferralCode(table.getReferralCode());
            entryEntity.setCallingCode(table.getCallingCode());
            entryEntity.setPhoneNumbe(table.getPhoneNumbe());
            userEntryService.add(entryEntity);
        }
    }

    @Override
    public ResultWithPagerDto<UserReferralDto> getReferralList(UserReferralEntity entity) {
        Integer count = userReferralMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        List<UserReferralDto> list = userReferralMapper.getReferralList(entity);
        return ResultWithPagerDto.success(pageDto, list);
    }

    private UserHireSearchListDto transformSearchListDto(UserHireTable userHireTable) {
        UserHireSearchListDto userHireSearchListDto = new UserHireSearchListDto();
        BeanUtils.copyProperties(userHireTable, userHireSearchListDto);
        return userHireSearchListDto;
    }


}
