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

import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.base.dao.servicePackage.ServicePackageDao;
import com.yihu.jw.base.dao.servicePackage.ServicePackageItemDao;
import com.yihu.jw.base.dao.servicePackage.ServicePackageSignRecordDao;
import com.yihu.jw.base.dao.system.SystemDictEntryDao;
import com.yihu.jw.base.dao.team.BaseTeamDao;
import com.yihu.jw.base.util.ConstantUtils;
import com.yihu.jw.entity.base.org.BaseOrgDO;
import com.yihu.jw.entity.base.servicePackage.ServicePackageDO;
import com.yihu.jw.entity.base.servicePackage.ServicePackageItemDO;
import com.yihu.jw.entity.base.system.SystemDictEntryDO;
import com.yihu.jw.entity.base.team.BaseTeamDO;
import com.yihu.jw.org.dao.BaseOrgDao;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.jw.utils.StringUtil;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 *
 * @author yeshijie on 2018/8/17.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ServicePackageService extends BaseJpaService<ServicePackageDO, ServicePackageDao> {

    private Logger logger = LoggerFactory.getLogger(ServicePackageService.class);

    @Autowired
    private ServicePackageDao servicePackageDao;
    @Autowired
    private ServicePackageItemDao servicePackageItemDao;
    @Autowired
    private BaseOrgDao baseOrgDao;
    @Autowired
    private BaseTeamDao baseTeamDao;
    @Autowired
    private SystemDictEntryDao systemDictEntryDao;

    @Autowired
    private ServicePackageSignRecordDao servicePackageSignRecordDao;

    @Transactional(rollbackFor = Exception.class)
    public JSONObject create(ServicePackageDO servicePackageDO){
        JSONObject result = new JSONObject();

        List<ServicePackageItemDO> itemDOList = servicePackageDO.getPackageItemDOList();
        if(itemDOList==null){
            servicePackageDO.setNum(0);
        }else {
            servicePackageDO.setNum(itemDOList.size());
        }

        if(StringUtil.isEmpty(servicePackageDO.getId())){
            //新增
            servicePackageDO.setCreateTime(new Date());
            servicePackageDO=servicePackageDao.save(servicePackageDO);
        }else{
            //修改
            ServicePackageDO oldDO = servicePackageDao.findOne(servicePackageDO.getId());
            oldDO.setName(servicePackageDO.getName());
            oldDO.setDel(servicePackageDO.getDel());
            oldDO.setType(servicePackageDO.getType());
            oldDO.setIntroduce(servicePackageDO.getIntroduce());
            oldDO.setOrgCode(servicePackageDO.getOrgCode());
            oldDO.setOrgName(servicePackageDO.getOrgName());
            servicePackageDO= servicePackageDao.save(oldDO);
            servicePackageItemDao.deleteByServicePackageId(oldDO.getId());
        }
        List<ServicePackageItemDO> itemDOs = new ArrayList<>();
        for (ServicePackageItemDO itemDO:itemDOList){
            BaseOrgDO baseOrgDO = baseOrgDao.findByCode(itemDO.getOrgCode());
            if (baseOrgDO!=null){
                itemDO.setOrgName(baseOrgDO.getName());
            }
            itemDO = servicePackageItemDao.findOne(itemDO.getId());
            ServicePackageItemDO itemDO1 = new ServicePackageItemDO();
            BeanUtils.copyProperties(itemDO,itemDO1);
            itemDO1.setId(null);
            itemDO1.setDictId(itemDO.getId());
            itemDO1.setServicePackageId(servicePackageDO.getId());
            itemDO1.setCreateTime(DateUtil.getStringDate());

            SystemDictEntryDO systemDictEntryDO = systemDictEntryDao.findByDictCodeAndCode("service_item",itemDO.getCode());
            if (systemDictEntryDO!=null){
                if (StringUtils.isNotBlank(systemDictEntryDO.getRemark())){//服务项所属专题不为空
                    itemDO1.setTopicItem(systemDictEntryDO.getRemark());
                }
            }
            itemDOs.add(itemDO1);
        }
        servicePackageItemDao.save(itemDOs);

        result.put("response", ConstantUtils.SUCCESS);
        result.put("msg","操作成功");
        return result;
    }

}
