using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Repository.MySql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{
    public class DicMapDeptServRepository : BaseRepositoryToASHermed_SinoInterface<Dic_Map_Dept_Serv>, IDicMapDeptServRepository
    {
        public int InsertDicMapDeptServ(Dic_Map_Dept_Serv mapDeptServ)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@" INSERT INTO `dic_map_dept_serv`(`DEPT_MAP_ID`, `HIS_SERV_ID`, `HIS_SERV_CODE`, `HIS_SERV_NAME`, `IS_DELETED`, `CN_NAME`, `EN_NAME`, `SORT`, `IS_SEND`, `IS_NA`) VALUES 
				(@DEPT_MAP_ID, @HIS_SERV_ID, @HIS_SERV_CODE, @HIS_SERV_NAME, @IS_DELETED, @CN_NAME, @EN_NAME, @SORT, @IS_SEND, @IS_NA) ");
            return Execute(sql.ToString(), mapDeptServ);
        }

        public Dic_Map_Dept_Serv FindByHosDeptWithHisCode(int aptHosId, string hisDeptCode, string hisServCode)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@hisDeptCode", hisDeptCode);
            parms.Add("@hosId", aptHosId);
            parms.Add("@hisServCode", hisServCode);
            var selectSql = @" select a.*
                                            from dic_map_dept_serv a,
                                                 dic_map_his_dept b,
                                                 dic_hos c
                                            where a.IS_DELETED = false
                                              and b.IS_DELETED = false
                                              and c.IS_DELETED = false
                                              and a.DEPT_MAP_ID = b.ID
                                              and b.HIS_DEPT_CODE = @hisDeptCode
                                              and c.HIS_HOS_ID = b.HIS_HOS_ID
                                              and c.ID = @hosId
                                              and a.HIS_SERV_CODE = @hisServCode
                                            limit 0,1";
            var result = Query<Dic_Map_Dept_Serv>(selectSql, parms);
            return result?.FirstOrDefault();
        }

        public Dic_Map_Dept_Serv FindByHisServCode(string hisServCode)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@hisServCode", hisServCode);
            var selectSql = @"select *
                                        from dic_map_dept_serv a
                                        where a.IS_DELETED = false
                                          and a.DEPT_MAP_ID is null
                                          and a.HIS_SERV_CODE = @hisServCode";
            var result = Query<Dic_Map_Dept_Serv>(selectSql, parms);
            return result?.FirstOrDefault();
        }

        public Dic_Map_Dept_Serv SelectByPrimaryKey(int aptProj)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@id", aptProj);
            var selectSql = @"select *
        from dic_map_dept_serv
        where ID = @id
        AND IS_DELETED = false";
            var result = Query<Dic_Map_Dept_Serv>(selectSql, parms);
            return result?.FirstOrDefault();
        }

        public Dic_Map_Dept_Serv SelectByHisKey(int aptProj)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@HIS_SERV_ID", aptProj);
            var selectSql = @"select *
        from dic_map_dept_serv
        where HIS_SERV_ID = @HIS_SERV_ID
        AND IS_DELETED = false";
            var result = Query<Dic_Map_Dept_Serv>(selectSql, parms);
            return result?.FirstOrDefault();
        }

    }
}
