using Sugar.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WebAPIBase.Utils;
public class SafeQualityCheckManager : DbContext<SafeQualityCheck>
{

    //当前类已经继承了 DbContext增、删、查、改的方法

    //这里面写的代码不会给覆盖,如果要重新生成请删除 SafeQualityCheckManager.cs

    protected NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
    /// <summary>
    /// 质量安全检查列表获取
    /// </summary>
    /// <param name="checkType">检查类型</param>
    /// <param name="searchValue">搜索条件</param>
    /// <returns></returns>
    public List<SafeQualityCheckDTO> GetSafeQualityCheckDTOs(string checkType, string searchValue)
    {

        List<SafeQualityCheckDTO> list;
        var sql = $"select * from SafeQualityCheck where 1=1";
        if (searchValue.IsNullOrEmpty())
        {
            sql += $" and checktype='{checkType}' order by checkcode desc";
            list = Db.Ado.SqlQuery<SafeQualityCheckDTO>(sql);
        }
        else
        {
            sql += $" and checktype='{checkType}' and projectname like '%{searchValue}%' order by checkcode desc";
            list = Db.Ado.SqlQuery<SafeQualityCheckDTO>(sql);
        }
        return list;
    }

    /// <summary>
    /// 获取质量安全检查实例通过主键
    /// </summary>
    /// <param name="checkCode"></param>
    /// <returns></returns>
    public SafeQualityCheckDTO GetSafeQualityCheckDTO(string checkCode)
    {
        var sql = $"select * from SafeQualityCheck where checkcode='{checkCode}'";
        var entity = Db.Ado.SqlQuerySingle<SafeQualityCheckDTO>(sql);
        return entity;
    }

    /// <summary>
    /// 获取质量安全明细列表
    /// </summary>
    /// <param name="checkCode"></param>
    /// <returns></returns>
    public List<SafeQualityCheckItem> GetSafeQualityCheckItems(string checkCode)
    {
        var sql = $"select * from SafeQualityCheckItem where checkcode='{checkCode}'";
        var list = Db.Ado.SqlQuery<SafeQualityCheckItem>(sql);
        return list;
    }
    /// <summary>
    /// 插入安全检查
    /// </summary>
    /// <param name="sc"></param>
    /// <param name="si"></param>
    /// <returns></returns>
    public List<SafeQualityResultDTO> InsertSafeQualityCheck(SafeQualityCheck sc, List<SafeQualityCheckItem> si)
    {
        var quaCheckCode = "";
        var safeCheckCode = "";
        var qualityList = new List<SafeQualityCheckItem>();    //质量检查项列表  10034
        var safeList = new List<SafeQualityCheckItem>();        //安全检查项列表   10035
        var list3 = new List<SafeQualityResultDTO>();
        foreach (var item in si)
        {
            if (item.DictionaryNameCode == "10034")
            {
                qualityList.Add(item);
            }
            else if (item.DictionaryNameCode == "10035")
            {
                safeList.Add(item);
            }
        }
        try
        {
            var manager = new UnitManager();
            Db.BeginTran();
            if (qualityList.Count > 0)    //质量检查入库
            {
                quaCheckCode = manager.GetSysCodeValue("SafeQualityCheck");
                sc.CheckCode = quaCheckCode;
                sc.CheckType = "QualityCheck";
                sc.Status = 0;
                sc.CheckID = manager.GetSysCodeValue(sc.CheckType);
                Db.Insertable(sc).ExecuteCommand();
                foreach (var item in qualityList)
                {
                    item.CheckItemCode = manager.GetSysCodeValue("SafeQualityCheckItem");
                    item.CheckCode = quaCheckCode;
                    Db.Insertable(item).ExecuteCommand();
                }
            }
            if (safeList.Count > 0)   //安全检查入库
            {
                safeCheckCode = manager.GetSysCodeValue("SafeQualityCheck");
                sc.CheckCode = safeCheckCode;
                sc.CheckType = "SafeCheck";
                sc.Status = 0;
                sc.CheckID = manager.GetSysCodeValue(sc.CheckType);
                Db.Insertable(sc).ExecuteCommand();
                foreach (var item in safeList)
                {
                    item.CheckItemCode = manager.GetSysCodeValue("SafeQualityCheckItem");
                    item.CheckCode = safeCheckCode;
                    Db.Insertable(item).ExecuteCommand();
                }
            }
            Db.CommitTran();
            if (qualityList.Count > 0 && safeList.Count > 0)
            {
                list3.Add(new SafeQualityResultDTO { Code = quaCheckCode, Type = "QualityCheck" });
                list3.Add(new SafeQualityResultDTO { Code = safeCheckCode, Type = "SafeCheck" });

            }
            else if (qualityList.Count > 0)
            {
                list3.Add(new SafeQualityResultDTO { Code = quaCheckCode, Type = "QualityCheck" });
            }
            else if (safeList.Count > 0)
            {
                list3.Add(new SafeQualityResultDTO { Code = safeCheckCode, Type = "SafeCheck" });
            }
            return list3;
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            throw;
        }

    }

    public string UpdateSafeQualityCheck(SafeQualityCheck sc, List<SafeQualityCheckItem> si)
    {
        var checkCode = "";
        var dictionaryNameCode = "";
        try
        {
            var manager = new UnitManager();
            var entity = SafeQualityCheckDb.GetById(sc.CheckCode);
            if (entity == null)
            {
                return "-1";   //不存在
            }
            if (entity.Status != 0)
            {
                return "-2";   //无权修改
            }
            if (entity.CheckType == "QualityCheck")
            {
                dictionaryNameCode = "10034";
            }
            else
            {
                dictionaryNameCode = "10035";
            }
            foreach (var item in si)
            {
                if (item.DictionaryNameCode != dictionaryNameCode)
                {
                    return "-3";   //所选检查选项与该选项类型不一致
                }
            }
            Db.BeginTran();
            checkCode = sc.CheckCode;

            Db.Updateable(sc).UpdateColumns(it => new { it.ProjectName, it.CheckDate }).ExecuteCommand();
            Db.Deleteable<SafeQualityCheckItem>().Where(m => m.CheckCode == checkCode).ExecuteCommand();
            foreach (var item in si)
            {
                item.CheckItemCode = manager.GetSysCodeValue("SafeQualityCheckItem");
                item.CheckCode = checkCode;
                Db.Insertable(item).ExecuteCommand();
            }

            Db.CommitTran();
            return checkCode;
        }
        catch (Exception ex)
        {
            Db.RollbackTran();
            throw;
        }

    }

}
