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

import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.base.dao.servicePackage.ServicePackageItemDao;
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.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by yeshijie on 2021/2/23.
 */
@Service
public class ServicePackageItemService extends BaseJpaService<ServicePackageItemDO, ServicePackageItemDao> {

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

    public JSONObject create(ServicePackageItemDO itemDO){
        JSONObject result = new JSONObject();
//         if(StringUtils.isEmpty(itemDO.getTeamCode())){
//            result.put("msg","请选择服务团队");
//            result.put("response", ConstantUtils.FAIL);
//            return result;
//        }

        BaseOrgDO baseOrgDO = baseOrgDao.findByCode(itemDO.getOrgCode());
        if (baseOrgDO!=null){
            itemDO.setOrgName(baseOrgDO.getName());
        }
//        BaseTeamDO baseTeamDO  = baseTeamDao.findOne(itemDO.getTeamCode());
//        if (baseTeamDO!=null){
//            itemDO.setTeamName(baseTeamDO.getName());
//        }
        if(StringUtil.isEmpty(itemDO.getId())){
            SystemDictEntryDO systemDictEntryDO = systemDictEntryDao.findByDictCodeAndCode("service_item",itemDO.getCode());
            if (systemDictEntryDO!=null){
                if (StringUtils.isNotBlank(systemDictEntryDO.getRemark())){//服务项所属专题不为空
                    itemDO.setTopicItem(systemDictEntryDO.getRemark());
                }
            }
            //新增
//            List<ServicePackageItemDO> itemDOList = servicePackageItemDao.findByTeamCode(itemDO.getTeamCode(),itemDO.getCode());
//            if(itemDOList!=null&&itemDOList.size()>0){
//                result.put("msg","该服务团队已存在此服务项，请勿重复添加");
//                result.put("response", ConstantUtils.FAIL);
//                return result;
//            }
            itemDO.setPrice(new BigDecimal(0));
            itemDO.setServicePackageId("system");
            itemDO.setCreateTime(DateUtil.getStringDate());
            servicePackageItemDao.save(itemDO);
        }else{
            //修改
            SystemDictEntryDO systemDictEntryDO = systemDictEntryDao.findByDictCodeAndCode("service_item",itemDO.getCode());
            if (systemDictEntryDO!=null){
                if (StringUtils.isNotBlank(systemDictEntryDO.getRemark())){//服务项所属专题不为空
                    itemDO.setTopicItem(systemDictEntryDO.getRemark());
                }
            }
            ServicePackageItemDO oldItemDO = servicePackageItemDao.findOne(itemDO.getId());
            oldItemDO.setName(itemDO.getName());
            oldItemDO.setDel(itemDO.getDel());
            oldItemDO.setCode(itemDO.getCode());
            oldItemDO.setFeeType(itemDO.getFeeType());
            oldItemDO.setIntroduce(itemDO.getIntroduce());
            servicePackageItemDao.save(oldItemDO);
        }

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

}
