using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using CallCenter.Utility;
using CallCenter.Utility.log;
using CallCenterApi.Interface.Controllers.Base;

namespace CallCenterApi.Interface.Controllers.workorder
{
    public class MaterialManageXLXController : BaseController
    {
        private AjaxResult ajresult = new AjaxResult();
        BLL.T_Cus_Customerrecords t_Cus_Customerrecords = new BLL.T_Cus_Customerrecords();
        public ActionResult AddMaterial()
        {
            Model.T_Cus_Customerrecords cusmodel = new Model.T_Cus_Customerrecords();
            string port_password = RequestString.GetFormString("port_password");   //加密信息(安全验证用)
            string uuid = RequestString.GetFormString("uuid");          //客户唯一标识（安全验证用）
                                                                       
            string materialcode = RequestString.GetFormString("materialcode");   //物料编码11位数字
            string materialname = RequestString.GetFormString("materialname");   //物料名称
            string model = RequestString.GetFormString("model");   //型号
            string specs = RequestString.GetFormString("specs");   //规格
            string level1 = RequestString.GetFormString("level1");   //一级分类
            string level2 = RequestString.GetFormString("level2");   //二级分类
            string level3 = RequestString.GetFormString("level3");   //三级分类
            string brand = RequestString.GetFormString("brand");   //品牌
            string materialeffect = RequestString.GetFormString("materialeffect");   //复合肥肥效
            string craft = RequestString.GetFormString("craft");   //工艺
            string pipeline = RequestString.GetFormString("pipeline");   //产品线
            string remark = RequestString.GetFormString("remark");   //备注
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            MaterialManageController cus = new MaterialManageController();
            Model.T_Wo_MaterialManage _Customer = new Model.T_Wo_MaterialManage();
            if (!string.IsNullOrEmpty(materialcode))
            {
                if (!cus.valcode(materialcode))
                {
                    ajresult.state = (int)CodeStatus.参数逻辑错误;
                    ajresult.message = "物料编号必须是11位数字";
                    return Process();
                }

                if (cus.getunique(0, materialcode))
                {
                    _Customer = new BLL.T_Wo_MaterialManage().GetModelList("F_IsDelete=0 and F_MaterialCode='" + materialcode + "'")
                 .FirstOrDefault(); ;
                }
            }
            else
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "物料编号不能为空";
                return Process();
            }
            if (string.IsNullOrEmpty(materialname))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "物料名称不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + materialcode, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
          
            if (!t_Cus_Customerrecords.ExistsCuscodeuuid(uuid))  //uuid不存在时,防止重复添加
            {
                int cusid = 0;
                if (_Customer!=null )
                {
                    cusid= cus.Add(materialcode, materialname, model, specs,
           level1, level2, level3, brand, materialeffect,
          craft, pipeline, remark, _Customer.F_Id );
                }
                else
                {
                    cusid = cus.Add(materialcode, materialname, model, specs,
             level1, level2, level3, brand, materialeffect,
            craft, pipeline, remark);

                }
                if (cusid == 0)
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }

                cusmodel.F_Cuscode = cusid.ToString();
                cusmodel.F_Cuscodeuuid = uuid;
                cusmodel.F_AddTime = DateTime.Now;
                cusmodel.F_Type = 3;
                int n = t_Cus_Customerrecords.Add(cusmodel);
                if (n <= 0)
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }

                ajresult.state = (int)CodeStatus.SUCCESS;
                ajresult.message = "操作成功";
                ajresult.data = cusid;  //订单号
                return Process();
            }
            else
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }

        }
        public ActionResult EditMaterial()
        {
            Model.T_Cus_Customerrecords cusmodel = new Model.T_Cus_Customerrecords();
            string port_password = RequestString.GetFormString("port_password");   //加密信息(安全验证用)
            string uuid = RequestString.GetFormString("uuid");          //客户唯一标识（安全验证用）
            int id = RequestString.GetInt("id", 0);   //客户档案id
            string materialcode = RequestString.GetFormString("materialcode");   //物料编码11位数字
            string materialname = RequestString.GetFormString("materialname");   //物料名称
            string model = RequestString.GetFormString("model");   //型号
            string specs = RequestString.GetFormString("specs");   //规格
            string level1 = RequestString.GetFormString("level1");   //一级分类
            string level2 = RequestString.GetFormString("level2");   //二级分类
            string level3 = RequestString.GetFormString("level3");   //三级分类
            string brand = RequestString.GetFormString("brand");   //品牌
            string materialeffect = RequestString.GetFormString("materialeffect");   //复合肥肥效
            string craft = RequestString.GetFormString("craft");   //工艺
            string pipeline = RequestString.GetFormString("pipeline");   //产品线
            string remark = RequestString.GetFormString("remark");   //备注
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            if (id <=0)
            {
                ajresult.state = (int)CodeStatus.ID不存在 ;
                ajresult.message = "请输入正确的ID";
                return Process();
              
            }
            Model.T_Wo_MaterialManage _Customer = new BLL.T_Wo_MaterialManage().GetModel(id);
            if (_Customer == null)
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "请输入正确的ID";
                return Process();
            }
            MaterialManageController cus = new MaterialManageController();
            if (!string.IsNullOrEmpty(materialcode))
            {
                if (!cus.valcode(materialcode))
                {
                    ajresult.state = (int)CodeStatus.参数逻辑错误;
                    ajresult.message = "物料编号必须是11位数字";
                    return Process();
                }

                if (cus.getunique(_Customer.F_Id , materialcode))
                {
                    ajresult.state = (int)CodeStatus.参数逻辑错误;
                    ajresult.message = "物料编号已被占用，请重新输入！";
                    return Process();
                }
            }
            else
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "物料编号不能为空";
                return Process();
            }
            if (string.IsNullOrEmpty(materialname))
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "物料名称不能为空";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + materialcode, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
            if (!t_Cus_Customerrecords.ExistsCuscodeuuid(uuid))  //uuid不存在时,防止重复添加
            {
                int cusid = cus.Add(materialcode, materialname, model, specs,
             level1, level2, level3, brand, materialeffect,
            craft, pipeline, remark,id );
                if (cusid == 0)
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }
                cusmodel.F_Cuscode = cusid.ToString();
                cusmodel.F_Cuscodeuuid = uuid;
                cusmodel.F_AddTime = DateTime.Now;
                cusmodel.F_Type = 4;
                int n = t_Cus_Customerrecords.Add(cusmodel);
                if (n <= 0)
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }

                ajresult.state = (int)CodeStatus.SUCCESS;
                ajresult.message = "操作成功";
                ajresult.data = cusid;  //订单号
                return Process();
            }
            else
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败,信息已存在";
                return Process();
            }

        }
        public ActionResult GetMaterial()
        {
            string port_password = RequestString.GetFormString("port_password");   //加密信息(安全验证用)
            string uuid = RequestString.GetFormString("uuid");          //客户唯一标识（安全验证用）
                                                                        //信息来源
            string materialcode = RequestString.GetFormString("materialcode");   //客户code10位数字
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }

            if (!Common.CodeValidate.ICodeValidate(uuid + materialcode, port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
            MaterialManageController cus = new MaterialManageController();
            if (!string.IsNullOrEmpty(materialcode))
            {
                if (!cus.valcode(materialcode))
                {
                    ajresult.state = (int)CodeStatus.参数逻辑错误;
                    ajresult.message = "物料编号必须是11位数字";
                    return Process();
                }

            }
            else
            {
                ajresult.state = (int)CodeStatus.必填字段为空;
                ajresult.message = "物料编号不能为空";
                return Process();
            }
            Model.T_Wo_MaterialManage _Customer = new BLL.T_Wo_MaterialManage().GetModelList("F_IsDelete=0 and F_MaterialCode='" + materialcode + "'")
                .FirstOrDefault();
            if (_Customer == null)
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "该物料不存在";
                return Process();
            }
            else
            {
                ajresult.state = (int)CodeStatus.SUCCESS;
                ajresult.message = "操作成功";
                ajresult.data = _Customer.F_Id ;  //订单号
                return Process();
            }

        }
        public ActionResult DelMaterial(string uuid, string port_password, int id = 0)
        {
            Model.T_Cus_Customerrecords cusmodel = new Model.T_Cus_Customerrecords();
            if (string.IsNullOrEmpty(uuid))
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "客户唯一标识不能为空";
                return Process();
            }
            else if (uuid.Length > 50)
            {
                ajresult.state = (int)CodeStatus.字符串超出长度;
                ajresult.message = "客户唯一标识超出长度";
                return Process();
            }
            if (id <= 0)
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "请输入正确的ID";
                return Process();
            }
            Model.T_Wo_MaterialManage _Customer = new BLL.T_Wo_MaterialManage().GetModel(id);
            if (_Customer == null)
            {
                ajresult.state = (int)CodeStatus.ID不存在;
                ajresult.message = "请输入正确的ID";
                return Process();
            }
            if (!Common.CodeValidate.ICodeValidate(uuid + _Customer.F_MaterialCode , port_password))
            {
                ajresult.state = (int)CodeStatus.安全验证未通过;
                ajresult.message = "安全验证未通过";
                return Process();
            }
            MaterialManageController cus = new MaterialManageController();
            if (cus.Del(id.ToString()))
            {
                cusmodel.F_Cuscode = id.ToString();
                cusmodel.F_Cuscodeuuid = uuid;
                cusmodel.F_AddTime = DateTime.Now;
                cusmodel.F_Type = 2;
                int n = t_Cus_Customerrecords.Add(cusmodel);
                if (n <= 0)
                {
                    ajresult.state = (int)CodeStatus.异常结果类型;
                    ajresult.message = "操作失败";
                    return Process();
                }
                ajresult.state = (int)CodeStatus.SUCCESS;
                ajresult.message = "操作成功";
                ajresult.data = "";  //订单号
                return Process();
            }
            else
            {
                ajresult.state = (int)CodeStatus.数据库错误;
                ajresult.message = "操作失败";
                return Process();
            }
        }


        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");

        }
        /// <summary>
        /// 验证物料编码为10位纯数字
        /// </summary>
        private bool valcode(string code)
        {
            var res = false;
            //11位数字
            if (code.Length == 10 && Regex.IsMatch(code, @"^\d{10}$"))
                res = true;
            return res;
        }
        /// <summary>
        /// 成功的消息格式化
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual ActionResult Process()
        {
            string methodName = new System.Diagnostics.StackTrace(true).GetFrame(1).GetMethod().Name;   //事件源
            LogInterface.WriteLog(this.GetType().ToString() + " -- " + methodName + "\r\n" + ajresult.state + "|" + ajresult.data + ajresult.message); //日志
            var jsonMsg = new AjaxResult { state = ajresult.state, message = ajresult.message, data = ajresult.data }.ToJson();
            return Content(jsonMsg);
        }
        /// <summary>
        /// 状态枚举
        /// </summary>
        enum CodeStatus
        {
            SUCCESS = 200,
            安全验证未通过 = 400,
            无法预测的程序错误 = 501,
            参数格式错误 = 100,
            必填字段为空 = 101,
            字符串超出长度 = 102,
            数据库错误 = 103,
            执行超时 = 104,
            参数逻辑错误 = 105,
            ID不存在 = 106,
            执行失败 = 108,
            用户已禁用 = 120,

            异常结果类型 = 500

        }
    }
}