package cn.com.ty.lift.ud.recruitmentInfos.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.com.ty.lift.ud.recruitmentInfos.controller.query.RecruitmentInfoQuery;
import cn.com.ty.lift.ud.recruitmentInfos.service.IRecruitmentInfoService;
import cn.com.ty.lift.ud.tableDict.mapper.TableDictMapper;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictDto;
import cn.com.ty.lift.ud.tableDict.mapper.entity.TableDictEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.recruitmentInfos.mapper.RecruitmentInfoMapper;
import cn.com.ty.lift.ud.recruitmentInfos.mapper.entity.RecruitmentInfoEntity;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 招聘信息 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-23
 */
@Service
@Transactional
public class RecruitmentInfoServiceImpl extends ServiceImpl<RecruitmentInfoMapper, RecruitmentInfoEntity>
    implements IRecruitmentInfoService {

    private @Autowired RecruitmentInfoMapper recruitmentInfoMapper;
    private @Autowired TableDictMapper tableDictMapper;

    @Override
    public RestResponse selectByExample(RecruitmentInfoQuery query) {
        IPage page = query.page();
        List<RecruitmentInfoEntity> list = recruitmentInfoMapper.selectByQuery(query.condition(), query);

        TableDictDto tdo1 = new TableDictDto();
        tdo1.setTableName("recruitment_info");
        tdo1.setField("edu_level");
        List<TableDictEntity> resultList1 = tableDictMapper.getConstantByTableAndColumn(tdo1);
        HashMap<String, String> map1 = new HashMap();
        for (TableDictEntity tdEntity : resultList1) {
            map1.put(tdEntity.getCode(), tdEntity.getVal());
        }
        for (RecruitmentInfoEntity rf : list) {
//            rf.setTypeName(map.get(rf.getCompany().getType().toString()));
            rf.setEduName(map1.get(rf.getEduLevel().toString()));
        }
        int total = recruitmentInfoMapper.countByQuery(query.condition());
        page.setRecords(list);
        page.setTotal(total);
        return RestResponse.success(page, "Queried successfully");
    }

    @Override
    public RecruitmentInfoEntity finOneById(Long id) {
        RecruitmentInfoEntity rf = recruitmentInfoMapper.finOneById(id);
        TableDictDto tdo1 = new TableDictDto();
        tdo1.setTableName("recruitment_info");
        tdo1.setField("edu_level");
        List<TableDictEntity> resultList1 = tableDictMapper.getConstantByTableAndColumn(tdo1);
        HashMap<String, String> map1 = new HashMap();
        for (TableDictEntity tdEntity : resultList1) {
            map1.put(tdEntity.getCode(), tdEntity.getVal());
        }
        rf.setEduName(map1.get(rf.getEduLevel().toString()));
        return rf;
    }

    @Override
    public void updateStatuzByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            RecruitmentInfoEntity bit = new RecruitmentInfoEntity();
            bit.setId(id);
            bit.setStatuz(status);
            recruitmentInfoMapper.updateById(bit);
        }
    }

    @Override
    public void updateHotFlagByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            RecruitmentInfoEntity bit = new RecruitmentInfoEntity();
            bit.setId(id);
            bit.setHotFlag(status);
            recruitmentInfoMapper.updateById(bit);
        }
    }

    @Override
    public RestResponse examineById(RecruitmentInfoEntity neDto) {
        if (neDto.getCheckFlag() == 1) {
            recruitmentInfoMapper.examineById(neDto.getId());
            return RestResponse.success(null, "审批通过");
        } else {
            // 修改状态为 驳回，并加上驳回理由
            recruitmentInfoMapper.updateInfoById(neDto.getId(), neDto.getReason());
            // todo 发送消息给该用户
            return RestResponse.fail(null, "审批驳回");
        }
    }

    @Override
    public void updateBrowseNum(Long id) {
        recruitmentInfoMapper.updateBrowseNum(id);
    }

}
