ProductService.cs 16.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hh.Mes.Service.WebService.Configure
{

    public class ProductService : RepositorySqlSugar<base_product_header>
    {
        #region 头表
        /// <summary>
        /// //获取列表
        /// </summary>
        public Response GetProductHeaderList(PageReq pageReq, base_product_header entity)
        {
            var result = new Response();
            string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? "  t1.createTime desc" : $"{pageReq.field} {pageReq.order} ";
            string sqlWhere = SqlWhere(entity);
            //工艺路线过滤已绑定的产品
            //string sqlWhereProcessRoute = " ";
            //if (entity.productCodeOrProductName == "no_process_product_rel") sqlWhereProcessRoute = " t1.productCode not in (select productCode from base_process_product_rel  ) ";
            var stringBuilder = new StringBuilder();
            //页码,页数
            //Exel ture 不分页
            if (!entity.Exel && pageReq != null)
            {
                stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
                stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
            }

            stringBuilder.AppendLine($@" select  t1.* 
                                         from [dbo].[base_product_header] t1    with(nolock)
                                         where {sqlWhere}  
                                         order by {orderBy} ");

            //Exel ture 不分页
            if (!entity.Exel)
            {
                stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
                stringBuilder.Append($" select rowTotal= count(*) from base_product_header  t1  with(nolock) where {sqlWhere}");
            }
            var parameters = new List<SugarParameter>(){
                new SugarParameter("@productCode",entity.productCode),
                new SugarParameter("@productCodeOrProductName",entity.productCodeOrProductName),
                new SugarParameter("@processCode",entity.processCode)
            };

            var ds = base.Context.Ado.GetDataSetAllAsync(stringBuilder.ToString(), parameters);
            result.Result = ds.Result.Tables[0];
            result.Count = entity.Exel ? (int)result.Result.Rows.Count : (int)ds.Result.Tables[1].Rows[0]["rowTotal"];
            return result;
        }

        public string SqlWhere(base_product_header model)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("1=1");
            if (!string.IsNullOrEmpty(model.productCode))
            {
                stringBuilder.Append(" and  t1.productCode like '%'+@productCode+'%' ");
            }
            if (!string.IsNullOrEmpty(model.processCode))
            {
                stringBuilder.Append(" and  t1.processCode like '%'+@processCode+'%' ");
            }
            //下拉弹出搜索框
            if (!string.IsNullOrEmpty(model.productCodeOrProductName) && model.productCodeOrProductName != "no_process_product_rel")
            {
                stringBuilder.Append(" and  (t1.productCode like   '%'+@productCodeOrProductName+'%' or  t1.productName like  '%'+@productCodeOrProductName+'%' )  ");
            }
            return stringBuilder.ToString();
        }

        public ISqlSugarClient GetContext()
        {
            return base.Context;
        }




        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_product_header model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                model.keys = Guid.NewGuid();
                if (string.IsNullOrEmpty(model.productCode))
                {
                    var prefix = base.GetDictionaryDictValue("base_product_header");
                    var maxValue = GetTableMaxValue<base_product_header>("id");
                    model.productCode = prefix + maxValue;
                }
                
                model.isDelete = SystemVariable.AddOrUpdateFlag;
                response.Status = Add(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        /// <summary>
        /// 根据主键数组 删除
        /// </summary>
        public dynamic DelByIds(Guid[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var Result = new Response();
                Result.Message = "";

                var proHead = Context.Queryable<base_product_header>().In(x => x.keys, ids).Select(x => x.processCode).ToList();
                //查看产品与工艺路线关系表是否存在,存在报错
                var exit = Context.Queryable<base_process_product_rel>().In(x => x.processCode, proHead).ToList();
                if (exit.Count > 0)
                {
                    Result.Code = 500;
                    Result.Message += $"工艺路线工艺路线编码 processCode 为【{proHead[0]}】的产品已经分配了工艺路线,不能删除!";
                    return Result;
                }
                Context.Deleteable<base_product_detail>().In(x => x.headKeys, ids).AddQueue();
                Context.Deleteable<base_product_header>().In(x => x.keys, ids).AddQueue();
                var result = Context.SaveQueues() > 0;
                if (!result)
                {
                    Result.Code = 500;
                    Result.Message = SystemVariable.dataActionError;
                }
                return Result;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_product_header model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                response.Status = Update(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }


        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public dynamic ExportData(base_product_header entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = GetProductHeaderList(null, entity);
                if (result.Count == 0)
                {
                    result.Result = "[]";
                    result.Count = 0;
                }
                return result;
            }, catchRetrunValue: "list");

        }

        #endregion

        #region 明细
        /// <summary>
        /// 查询方法明细
        /// </summary>
        public Response LoadDesc(PageReq pageReq, base_product_detail model)
        {
            var totalCount = 0;
            var result = new Response();
            var query = Context.Queryable<base_product_detail, base_material>((t1, t2) =>
                                  new JoinQueryInfos(JoinType.Left, t1.materialCode == t2.materialCode))
                                 .Select((t1, t2) => new
                                 {
                                     t1.id,
                                     t1.materialCode,
                                     t1.headKeys,
                                     t1.createBy,
                                     t1.createTime,

                                     t2.materialName,
                                     t2.mtClassify,
                                     t2.mtTypeCode,
                                     t2.unitCode,
                                     t2.thickness,
                                     t2.diameter,
                                     t2.types
                                 });

            var data = model.headKeyList != null ? query.In(t1 => t1.headKeys, model.headKeyList).ToList() :
                       query.Where(t1 => t1.headKeys == model.headKeys).OrderBy(t1 => t1.id).ToOffsetPage(pageReq.page, pageReq.limit, ref totalCount);
            //页码,页数
            result.Result = data;
            result.Count = totalCount;
            return result;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        public dynamic InsDesc(base_product_detail model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.createBy = sysWebUser?.Account;
                model.createTime = DateTime.Now;
                response.Status = Add(model);
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        /// <summary>
        /// 编辑方法
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>

        public dynamic UpdDesc(base_product_detail model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = sysWebUser?.Account;
                model.updateTime = DateTime.Now;
                response.Status = base.Context.Updateable(model).IgnoreColumns(it => new { it.createBy, it.createTime }).ExecuteCommand() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="ids">需要删除的id</param>
        /// <returns></returns>
        public dynamic DelByIdsDesc(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                Context.Deleteable<base_product_detail>().In(ids).AddQueue();
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;

            });
        }

        #endregion}


        /// <summary>
        /// 获取绑定在该 产品的工艺路线的产品
        /// </summary>
        public dynamic GetProductByProductCode(string processCode)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Result = Context.Queryable<base_process_product_rel>().Where(x => x.processCode == processCode).Select(x => new { x.productCode }).ToList();
                return response;
            });
        }

        /// <summary>
        ///工艺路线-产品关系表 的新增与删除
        /// </summary>
        /// <returns></returns>
        public dynamic OperateProduct(string processCode, string processName, string productCode, Guid processHeadKeys, string lineCode, bool checkeds)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                string[] strArray = productCode.Split(new char[] { ',' });
                foreach (var item in strArray)
                {
                    //更新产品表base_product_header
                    var bph = new base_product_header
                    {
                        updateBy = sysWebUser?.Account,
                        updateTime = DateTime.Now
                    };
                    if (checkeds)
                    {
                        var isExt = Context.Queryable<base_process_product_rel>().Any(x => x.productCode == item);
                        if (isExt)
                        {
                            var bppr=Context.Queryable<base_process_product_rel>().First(x=>x.productCode==productCode);
                            return response.ResponseError($"产品编码:【{productCode}】与工艺路线:【{bppr.processCode}】已绑定工艺路线产品关系(工艺和产品一对多,一个产品只能有一个工艺)");
                        }
                        var model = new base_process_product_rel
                        {
                            keys = Guid.NewGuid(),
                            processHeadKeys = processHeadKeys,
                            lineCode = lineCode,
                            processCode = processCode,
                            processName = processName,
                            productCode = item,
                            createBy = sysWebUser?.Account,
                            createTime = DateTime.Now
                        };
                        Context.Insertable(model).AddQueue();

                        //更新产品表base_product_header
                        bph.processHeadKeys= processHeadKeys;
                        bph.processCode= processCode;
                    }
                    else
                    {
                        Context.Deleteable<base_process_product_rel>().Where(x => x.productCode == item && x.processCode == processCode).AddQueue();

                        //取消复选框,更新产品表base_product_header,表中工艺路线列赋空
                        bph.processHeadKeys = Guid.Empty;
                        bph.processCode = "";
                    }

                    Context.Updateable(bph).UpdateColumns(x => new { x.processCode, x.processHeadKeys, x.updateBy, x.updateTime }).Where(x => x.productCode == productCode).AddQueue();
                }
                response.Status = Context.SaveQueues() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response.Status ? response.ResponseSuccess() : response.ResponseError();
            });
        }


        /// <summary>
        /// 产品明细 分配物料
        /// </summary>
        /// <param name="teamCode"></param>
        /// <param name="userId"></param>
        /// <param name="flag">(check选择,uncheck 取消)</param>
        public dynamic AssignMaterial(string materialCodes, Guid proHeadKeys, bool checkeds)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                string[] strArray = materialCodes.Split(new char[] { ',' });
                if (checkeds)
                {
                    foreach (var item in strArray)
                    {
                        Context.Deleteable<base_product_detail>().Where(x => x.materialCode == item && x.headKeys == proHeadKeys).ExecuteCommand();
                        var model = new base_product_detail
                        {
                            materialCode = item,
                            headKeys = proHeadKeys,
                            createBy = sysWebUser?.Account,
                            createTime = DateTime.Now
                        };
                        Context.Insertable(model).AddQueue();
                    }
                    response.Status = Context.SaveQueues() > 0;
                    if (!response.Status)
                    {
                        response.Message = SystemVariable.dataActionError;
                    }
                }
                else
                {
                    foreach (var item in strArray)
                    {
                        Context.Deleteable<base_product_detail>().Where(x => x.headKeys == proHeadKeys).AddQueue();
                    }
                    response.Status = Context.SaveQueues() > 0;
                    if (!response.Status)
                    {
                        response.Message = SystemVariable.dataActionError;
                    }
                }
                return response;
            });
        }

    }

}