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


import cn.hutool.log.Log;
import com.bonus.boot.manager.basic.dao.PhyManagementDao;
import com.bonus.boot.manager.basic.entity.PhyCombinBean;
import com.bonus.boot.manager.basic.service.PhyManagementService;
import com.bonus.boot.manager.manager.dao.UtilDao;
import com.bonus.boot.manager.manager.utils.StringHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service("phyManagementService")
public class PhyManagementServiceImpl implements PhyManagementService {
    @Resource
    private PhyManagementDao dao;
    @Resource
    private UtilDao utilDao;
    @Override
    public List<PhyCombinBean> getList(PhyCombinBean o) {
        return dao.getList(o);
    }

    @Override
    public int addInfo(PhyCombinBean bean) {
        int num = 0;
        if ("个性化套餐".equals(bean.getCombinTypeName())){
            num = dao.determineIfExists(bean.getCombinSex(),bean.getIsHighPosition(),bean.getPersonType(),bean.getCheckType(), "");
            if(num > 0){
                return 3;
            }
        }else if ("标准套餐".equals(bean.getCombinTypeName())){
            bean.setCombinSex("3");
            bean.setCheckType("");
            bean.setPersonType("");
            bean.setIsHighPosition("3");
            //去查询对应的标准套餐id
            String standId =utilDao.getstandId();
            num = utilDao.determineIfExists("pm_base_combination","comb_type",standId,"");
            if (num > 0){
                return 4;
            }
        }else if ("职业健康套餐".equals(bean.getCombinTypeName())){
            bean.setCombinSex("3");
            bean.setCheckType("");
            bean.setPersonType("");
            bean.setIsHighPosition("3");
        }
        num = dao.insertBasicData(bean);
        if (num == 1){
            String[] selectId = bean.getSelectId().split(",");
            String[] price = bean.getPriceList().split(",");
            String[] combinName = bean.getCombinContent().split(",");
            String[] contentList = bean.getContentList().split("]],");
            for (int i = 0; i < selectId.length; i++) {
                bean.setSelectId(selectId[i]);
                bean.setCombinPrice(price[i]);
                bean.setCombinName(combinName[i]);
                bean.setContentList(contentList[i]);
                dao.addInfo(bean);
            }
            num = 1;
        }else{
            System.out.println("新增基础数据失败");
        }
        return num;
    }

    @Override
    public int updateInfo(PhyCombinBean bean) {
        int num = 0;
        if ("个性化套餐".equals(bean.getCombinTypeName())){
            num = dao.determineIfExists(bean.getCombinSex(),bean.getIsHighPosition(),bean.getPersonType(),bean.getCheckType(), bean.getId());
            if(num > 0){
                return 3;
            }
        }else if ("标准套餐".equals(bean.getCombinTypeName())){
            bean.setCombinSex("3");
            bean.setCheckType("");
            bean.setPersonType("");
            bean.setIsHighPosition("3");
            num = utilDao.determineIfExists("pm_base_combination","comb_type","1",bean.getId());
            if (num > 0){
                return 4;
            }
        }else if ("职业健康套餐".equals(bean.getCombinTypeName())){
            bean.setCombinSex("3");
            bean.setCheckType("");
            bean.setPersonType("");
            bean.setIsHighPosition("3");
        }
        num = dao.updateInfo(bean);
        if (num == 1){
            String[] selectId = bean.getSelectId().split(",");
            String[] price = bean.getPriceList().split(",");
            String[] combinName = bean.getCombinContent().split(",");

            if (!StringHelper.isEmpty(bean.getContentList())){
                String[] contentList = bean.getContentList().split("]],");
                dao.delete(bean);
                for (int i = 0; i < selectId.length; i++) {
                    bean.setSelectId(selectId[i]);
                    bean.setCombinPrice(price[i]);
                    bean.setCombinName(combinName[i]);
                    bean.setContentList(contentList[i]);
                    dao.addInfo(bean);
                }
            }else{
                System.out.println("未进行套餐内容修改");
            }
            num = 1;
        }else{
            System.out.println("新增基础数据失败");
        }
        return num;
    }

    @Override
    public PhyCombinBean getListById(String id) {
        return dao.getListById(id);
    }

    @Override
    @Transactional
    public int delById(PhyCombinBean bean) {
        return dao.delById(bean);
    }
}
