package cn.hnthyy.thmz.service.impl.his;

import cn.hnthyy.thmz.entity.his.YpConfig;
import cn.hnthyy.thmz.entity.his.YpInPlan;
import cn.hnthyy.thmz.entity.his.zd.YpZdDictBase;
import cn.hnthyy.thmz.mapper.his.YpConfigMapper;
import cn.hnthyy.thmz.mapper.his.YpInPlanMapper;
import cn.hnthyy.thmz.service.his.YpInPlanService;
import cn.hnthyy.thmz.vo.YpInPlanDrugVo;
import cn.hnthyy.thmz.vo.YpInPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: WANGJIALIANG
 * @time: 2021/7/21 10:35
 */
@Service
public class YpInPlanServiceImpl implements YpInPlanService {

    @Autowired
    private YpInPlanMapper ypInPlanMapper;
    @Autowired
    private YpConfigMapper ypConfigMapper;

    @Override
    public List<YpInPlanVo> queryYpInPlanPage(YpInPlanVo ypInPlanVo) {
        return ypInPlanMapper.selectYpInPlanPage(ypInPlanVo);
    }

    @Override
    public int countYpInPlan(YpInPlanVo ypInPlanVo) {
        return ypInPlanMapper.countYpInPlan(ypInPlanVo);
    }

    @Override
    public List<YpZdDictBase> queryYpZdDictBase(String searchText, String groupYk) {
        return ypInPlanMapper.selectYpZdDictBase(searchText,groupYk);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int saveYpInPlan(List<YpInPlan> ypInPlans) {
        YpConfig ypConfig=ypConfigMapper.selectYpConfig();
        ypConfig.setYfPlanNo(ypConfig.getYfPlanNo()+1);
        ypConfigMapper.updateYpConfig(ypConfig);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String drawNo = sdf.format(new Date())+ypConfig.getYfPlanNo();
        int i = 0;
        for (YpInPlan ypInPlan:ypInPlans) {
            ypInPlan.setDrawNo(drawNo);
            i = i + ypInPlanMapper.insertYpInPlan(ypInPlan);
        }
        return i;
    }

    @Override
    public List<YpInPlanDrugVo> queryYpInPlanDrug(String drawNo) {
        return ypInPlanMapper.selectYpInPlanDrug(drawNo);
    }
}
