package com.yihu.jw.base.service.voluntary;

import com.yihu.jw.base.dao.voluntary.VoluntaryRecruitmentArrangeDao;
import com.yihu.jw.base.dao.voluntary.VoluntaryRecruitmentCompanyDao;
import com.yihu.jw.base.dao.voluntary.VoluntaryRecruitmentPeopleDao;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentArrangeDO;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentCompanyDO;
import com.yihu.jw.entity.voluntary.VoluntaryRecruitmentPeopleDO;
import com.yihu.jw.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/8/1
 * @Description:
 */
@Service
public class VoluntaryRecruitmentService {

    @Autowired
    private VoluntaryRecruitmentCompanyDao companyDao;
    @Autowired
    private VoluntaryRecruitmentArrangeDao arrangeDao;
    @Autowired
    private VoluntaryRecruitmentPeopleDao peopleDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 人员安排
     */
    public void arrange(String companyId,String day,String time,String peoples,String station){
        List<VoluntaryRecruitmentArrangeDO> arrangeDOs = new ArrayList<>();
        String people[] = peoples.split(",");
        for (String peo:people){
            String p[] = peo.split(":");
            VoluntaryRecruitmentArrangeDO arrangeDO = new VoluntaryRecruitmentArrangeDO();
            arrangeDO.setCompanyId(companyId);
            arrangeDO.setDay(day);
            arrangeDO.setPeopleId(p[0]);
            arrangeDO.setPeopleName(p[1]);
            arrangeDO.setStation(station);
            arrangeDO.setTime(time);
            arrangeDOs.add(arrangeDO);
        }
        arrangeDao.save(arrangeDOs);
    }

    /**
     * 企业人员安排信息
     */
    public List<Map<String,Object>> arrangeInfoList(String companyId){
        String sql = " select day,time,count(people_id) from voluntary_recruitment_arrange where company_id = '"+companyId+"' " +
                " group by day,time order by day,time asc ";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        return result;
    }

    public List<VoluntaryRecruitmentArrangeDO> arrangeInfoDetail(String companyId,String day){
        String sql = "select ar.*,po.company_name,po.phone,po.idcard,po.vaccination,po.experience from " +
                " voluntary_recruitment_arrange ar  INNER JOIN voluntary_recruitment_people po on ar.people_id = po.id " +
                " where ar.company_id = '"+companyId+"' " ;
        if (StringUtils.isNoneBlank(day)){
            sql += " and ar.day = '"+day+"' ";
        }
        sql += " order by ar.day,ar.time asc ";
        List<VoluntaryRecruitmentArrangeDO> result = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(VoluntaryRecruitmentArrangeDO.class));
        return result;
    }

    public List<VoluntaryRecruitmentPeopleDO> companyPeopleUnArrange(String companyId,String day,String time){
        String sql =" select po.* from voluntary_recruitment_people po  LEFT JOIN voluntary_recruitment_arrange ar " +
                " on ar.people_id = po.id where ar.company_id = '"+companyId+"'  and ar.`day`='"+day+"' " +
                "  and ar.time<> '"+time+"' group by ar.day,ar.time order by ar.day,ar.time asc ";
        List<VoluntaryRecruitmentPeopleDO> result = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(VoluntaryRecruitmentPeopleDO.class));
      return result;
    }

    public VoluntaryRecruitmentCompanyDO findByContactsAndPhone(String contacts,String phone){
        List<VoluntaryRecruitmentCompanyDO> companyDOs = companyDao.findByContactsAndPhone(contacts,phone);
        if(companyDOs!=null&&companyDOs.size()>0){
            return companyDOs.get(0);
        }
        return null;
    }

    /**
     * 企业报名
     */
    public String add(VoluntaryRecruitmentCompanyDO companyDO){
        List<VoluntaryRecruitmentPeopleDO> peopleDOs = new ArrayList<>();
        if(StringUtil.isBlank(companyDO.getId())){
            if(companyDao.existsByName(companyDO.getName())){
                return "该企业已报名，请勿重复报名";
            }

            List<VoluntaryRecruitmentCompanyDO> companyDOs = companyDao.findByContactsAndPhone(companyDO.getContacts(),companyDO.getPhone());
            if(companyDOs!=null&&companyDOs.size()>0){
                return "该联系人已被其他企业添加";
            }

            List<VoluntaryRecruitmentPeopleDO> peopleDOList = companyDO.getPeopleDOList();
            if(peopleDOList!=null){
                addPeople(peopleDOList,peopleDOs,companyDO);
                return null;
            }
            return null;
        }

        List<VoluntaryRecruitmentPeopleDO> peopleDOList = companyDO.getPeopleDOList();
        if(peopleDOList==null){
            return "请添加人员";
        }

        addPeople(peopleDOList,peopleDOs,companyDO);
        return null;
    }

    public void addPeople(List<VoluntaryRecruitmentPeopleDO> peopleDOList,List<VoluntaryRecruitmentPeopleDO> peopleDOs,VoluntaryRecruitmentCompanyDO companyDO){

        if(!StringUtil.isBlank(companyDO.getId())){
            VoluntaryRecruitmentCompanyDO old = companyDao.findOne(companyDO.getId());
            companyDO = old;
        }else {
            companyDO.setNum(0);
            companyDao.save(companyDO);
        }
        for (VoluntaryRecruitmentPeopleDO peopleDO:peopleDOList){
            if(peopleDao.existsByPhone(peopleDO.getPhone())){
                continue;
            }
            peopleDO.setCompanyId(companyDO.getId());
            peopleDO.setCompanyName(companyDO.getName());
            peopleDOs.add(peopleDO);
        }
        if(peopleDOs.size()>0){
            peopleDao.save(peopleDOs);
        }
        companyDO.setNum(companyDO.getNum()+peopleDOs.size());
        companyDao.save(companyDO);
    }


}
