package com.bonus.boot.manager.physical.service.impl;

import com.bonus.boot.manager.basic.entity.MapBean;
import com.bonus.boot.manager.basic.entity.PersonnelLibraryBean;
import com.bonus.boot.manager.manager.entity.R;
import com.bonus.boot.manager.manager.utils.StringHelper;
import com.bonus.boot.manager.manager.utils.UserUtil;
import com.bonus.boot.manager.physical.dao.RetirementDao;
import com.bonus.boot.manager.physical.service.RetiremenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("retiremenService")
public class RetiremenServiceImpl implements RetiremenService {

    @Resource
    private RetirementDao retirementDao;

    @Override
    public List<PersonnelLibraryBean> getList(PersonnelLibraryBean o) {
        return retirementDao.getList(o);
    }

    @Override
    @Transactional
    public R addReservationData(PersonnelLibraryBean o) {
        String personArray = o.getPersonStr();
        String tcManId = o.getTcManId();
        String tcWomanId = o.getTcWomanId();
        String tcManName = o.getTcManName();
        String tcWomanName = o.getTcWomanName();
        List<PersonnelLibraryBean> personList = retirementDao.getPersonData(o);
        System.err.println("personList="+personList);
        try {
            for (PersonnelLibraryBean bean: personList) {
                String tcId = "";
                bean.setId(o.getId());
                bean.setReservationTime(o.getReservationTime());
                bean.setOperatePerson(UserUtil.getLoginUser().getId()+"");
                bean.setIfCancel("2");
                bean.setCheckContentId("1");
                bean.setIfCareerAppoint("1");
                if("0".equals(bean.getSex()) || "男".equals(bean.getSex())){
                    bean.setCombName(tcManName);
                    tcId = tcManId;
                }else{
                    bean.setCombName(tcWomanName);
                    tcId = tcWomanId;
                }
                retirementDao.addReservationData(bean);
                retirementDao.addHistoryData(tcId,bean.getTjId());

            }
            return R.okTable("添加成功！", 1);
        }catch (Exception e){
            return R.okTable("添加失败！", 0);
        }
    }

    @Override
    public int cancelReservation(String ifCancel, String id) {
        return retirementDao.cancelReservation(ifCancel, id);
    }


    @Override
    public MapBean getRetirementNum(MapBean o) {
        MapBean mapBean = retirementDao.getRetirementNum(o);
        System.err.println("mapBean="+mapBean);
        if(mapBean !=null){
            if(StringHelper.isEmpty(mapBean.getKey())){
                mapBean.setKey("50");
            }

        }else {
            mapBean = new MapBean();
            mapBean.setKey("50");
            mapBean.setValue("0");
        }
        return mapBean  ;
    }
}
