package com.bzd.system.service;

import com.bzd.common.config.dao.DaoBase;
import com.bzd.common.config.dao.DaoSupport;
import com.bzd.common.config.dao.PageData;
import com.bzd.common.core.domain.entity.DtpDeliveryPrint;
import com.bzd.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.List;
import java.util.Map;

@Service
public class DTPService {
    @Autowired
    private DaoBase dao;

    @Resource(name = "daoSupport")
    private DaoSupport daoSupport;
    /**
     * 处方列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> findForList(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("DTPMapper.RecipeRegisterList", pd);
    }
    /**
     * 处方信息
     * @param pd
     * @return
     * @throws Exception
     */
    public Object findOne(final PageData pd) throws Exception {
        return  daoSupport.findForObject("DTPMapper.selectOne", pd);
    }
    /**
     * 处方登记 添加
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer save(final PageData pd) throws Exception {

                pd.put("orderId","1111");//订单编号
                pd.put("saleOrderNumber","22323421");//销售单号
                LocalDateTime now = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2024-01-01 10:00:00
                String formattedOrderTime = now.format(formatter);
                pd.put("orderTime",formattedOrderTime);//订单时间
                pd.put("prescriptionNumber","22323422223321");//处方单号

        return daoSupport.save("DTPMapper.insertRecipe", pd);
    }
    /**
     * 处方删除
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer del(PageData pd) throws Exception {
        return daoSupport.delete("DTPMapper.deleteRecipe", pd);
    }
    /**
     * 处方修改
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer update(PageData pd) throws Exception {
        return daoSupport.delete("DTPMapper.updateRecipe", pd);
    }
    /**
     * 校验患者姓名和电话
     * @param pd
     * @return
     * @throws Exception
     */
    public boolean checkPatientNameAndPhoneUnique(PageData pd) throws Exception {

        Object obj=   daoSupport.findForObject("DTPMapper.checkPatientNameAndPhoneUnique", pd);
        return StringUtils.isNull(obj);
    }

    /**
     * 冷链配送列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> findColdForList(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("DTPMapper.DistributionOrderList", pd);
    }

    /**
     * 冷链配送登记 添加
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer saveCold(final PageData pd) throws Exception {

        pd.put("orderNumber","1113333");//订单编号
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2024-01-01 10:00:00
        String formattedOrderTime = now.format(formatter);
        pd.put("create_time",formattedOrderTime);//订单时间
        pd.put("creationTime",formattedOrderTime);//订单创建时间
        pd.put("createdBy","admin");//订单创建时间
        return daoSupport.save("DTPMapper.insertDistributionOrder", pd);
    }
    /**
     * 配送订单修改
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer editColdOrder(PageData pd) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2024-01-01 10:00:00
        String formattedOrderTime = now.format(formatter);
        pd.put("updatedTime",formattedOrderTime);//订单时间
        return daoSupport.update("DTPMapper.updateDistributionOrder", pd);
    }
    /**
     * 配送订单软删除
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer softDeleteColdOrder(PageData pd) throws Exception {
        return daoSupport.delete("DTPMapper.softDeleteColdOrder", pd);
    }

    /**
     * 配送单据打印列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> listPrint(final PageData pd) throws Exception {

        return (List<PageData>) daoSupport.findForList("DTPMapper.listPrintPd", pd);
    }
    /**
     * 配送单据打印导出
     * @param pd
     * @return
     * @throws Exception
     */
    public List<DtpDeliveryPrint> listPrintReport(final PageData pd) throws Exception {
        return (List<DtpDeliveryPrint>) daoSupport.findForList("DTPMapper.listPrintEntity", pd);
    }
    /**
     * 配送单据打印 添加
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer savePrint(final PageData pd) throws Exception {

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2024-01-01 10:00:00
        String formattedOrderTime = now.format(formatter);
        pd.put("create_time",formattedOrderTime);//订单时间
        pd.put("creationTime",formattedOrderTime);//订单创建时间
        pd.put("createdBy","admin");//订单创建人
        pd.put("businessId","10359");//业务ID
        pd.put("status","待打印");//打印状态

        return daoSupport.save("DTPMapper.savePrint", pd);
    }
    /**
     * 配送单据打印修改
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer editPrint(PageData pd) throws Exception {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//2024-01-01 10:00:00
        String formattedOrderTime = now.format(formatter);
        pd.put("updatedTime",formattedOrderTime);//订单时间
        return daoSupport.update("DTPMapper.editPrint", pd);
    }
    /**
     * 配送单据打印软删除
     * @param pd
     * @return
     * @throws Exception
     */
    public Integer DelPrint(PageData pd) throws Exception {
        return daoSupport.delete("DTPMapper.DelPrint", pd);
    }



    /**配置信息查询模块**/
    /**
     * 一店一目标签列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> findstoreTagList(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("DTPMapper.StoreProductList", pd);
    }
    /**
     * DTP医院列表
     * @param pd
     * @return
     * @throws Exception
     */
    public List<PageData> findHospitalList(final PageData pd) throws Exception {
        return (List<PageData>) daoSupport.findForList("DTPMapper.HospitalList", pd);
    }
}
