using Competition.Common.Util; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; namespace CompetitionAPI.Controllers { /// /// 客户收资 /// [Route("api/[controller]")] [ApiController] public class ClientCollectController : BaseHandlerController { Competition.Mysql.BLL.app_client_collect bll = new Competition.Mysql.BLL.app_client_collect(); Competition.Mysql.BLL.app_struct_address bll_address = new Competition.Mysql.BLL.app_struct_address(); private readonly IWebHostEnvironment _env; public static readonly string Dir= "Client"; public ClientCollectController(IHttpContextAccessor IHttpContextAccessor, IWebHostEnvironment env, IConfiguration iconfiguration) { Configuration = iconfiguration; _env = env; context = IHttpContextAccessor.HttpContext!; CrossDomain(); } public bool saveFile(string Dir,IFormFile file,string fileId,out string server_path) { server_path = ""; if (null== file) { return false; } var directory = Path.Combine(_env.WebRootPath, Dir); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } var extension = file.FileName.Substring(file.FileName.LastIndexOf(".")); string NewfileName = fileId + extension; //path = Path.Combine(directory, file.FileName); string path = Path.Combine(directory, NewfileName); using (var stream = new FileStream(path, FileMode.Create)) { file.CopyTo(stream); } server_path = "~/"+Dir + "/" + NewfileName; return true; } //[Authorize] [HttpPost, DisableRequestSizeLimit] [Route("Add")] public JsonResult Add() { try { JsonResult ret; var data = GetValue("data"); if (string.IsNullOrEmpty(data)) { ret = GetResult(false, "data不能为空"); return ret; } var model = new Competition.Mysql.Model.app_client_collect(); try { model = JsonConvert.DeserializeObject(data)!; } catch (Exception ex) { ret = GetResult(false, "data转换错误:" + ex.Message); return ret; } if (model != null) { if (string.IsNullOrWhiteSpace(model.user_name)) { return GetResult(false, "用电户名称不能为空"); } if (string.IsNullOrWhiteSpace(model.hangye_type)) { return GetResult(false, "行业分类不能为空"); } if (string.IsNullOrWhiteSpace(model.power_voltage)) { return GetResult(false, "供电电压不能为空"); } if (string.IsNullOrWhiteSpace(model.power_type)) { return GetResult(false, "用电类别不能为空"); } if (string.IsNullOrWhiteSpace(model.city_type)) { return GetResult(false, "城乡类别不能为空"); } model.id = Tool.GetId("320"); //营业照 var file_business_photo = GetFile("business_photo"); if (saveFile(Dir,file_business_photo, Tool.GetNextId(model.id,1), out string path)) { model.business_photo = path; } //房产证 var file_house_photo = GetFile("house_photo"); if (saveFile(Dir,file_house_photo, Tool.GetNextId(model.id, 2), out string path_house_photo)) { model.house_photo = path_house_photo; } //身份证正面 var file_sfz_front = GetFile("sfz_front"); if (saveFile(Dir, file_sfz_front, Tool.GetNextId(model.id, 3), out string path_sfz_front)) { model.sfz_front = path_sfz_front; } //身份证反面 var file_sfz_back = GetFile("sfz_back"); if (saveFile(Dir, file_sfz_back, Tool.GetNextId(model.id, 4), out string path_sfz_back)) { model.sfz_back = path_sfz_back; } //授权委托书 var file_apply_book = GetFile("apply_book"); if (saveFile(Dir, file_apply_book, Tool.GetNextId(model.id, 5), out string path_apply_book)) { model.apply_book = path_apply_book; } //经办人身份证 var file_operator_idcard = GetFile("operator_idcard"); if (saveFile(Dir, file_operator_idcard, Tool.GetNextId(model.id, 6), out string path_operator_idcard)) { model.operator_idcard = path_operator_idcard; } //其它证件1 var file_zj1 = GetFile("zj1"); if (saveFile(Dir, file_zj1, Tool.GetNextId(model.id, 7), out string path_zj1)) { model.zj1 = path_zj1; } //其它证件2 var file_zj2 = GetFile("zj2"); if (saveFile(Dir, file_zj2, Tool.GetNextId(model.id, 8), out string path_zj2)) { model.zj2 = path_zj2; } model.create_time = DateTime.Now; if (bll.Add(model)) { return GetResult(true, model, ""); } else { return GetResult(false, "添加失败"); } } else { return GetResult(false, "请求参数不能为空"); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return GetResult(false, "发生错误,请联系管理员"); } } //[Authorize] [HttpPost] [Route("Update")] public JsonResult Update() { try { JsonResult ret; var id = GetValue("id"); if (string.IsNullOrEmpty(id)) { ret = GetResult(false, "id不能为空"); return ret; } var obj = bll.GetModel(id); if (null == obj) { ret = GetResult(false, "客户收资信息不存在 id:" + id); return ret; } var data = GetValue("data"); if (string.IsNullOrEmpty(data)) { ret = GetResult(false, "data不能为空"); return ret; } var model = new Competition.Mysql.Model.app_client_collect(); try { model = JsonConvert.DeserializeObject(data)!; } catch (Exception ex) { ret = GetResult(false, "data转换错误:" + ex.Message); return ret; } if (model != null) { if (string.IsNullOrWhiteSpace(model.user_name)) { return GetResult(false, "用电户名称不能为空"); } if (string.IsNullOrWhiteSpace(model.hangye_type)) { return GetResult(false, "行业分类不能为空"); } if (string.IsNullOrWhiteSpace(model.power_voltage)) { return GetResult(false, "供电电压不能为空"); } if (string.IsNullOrWhiteSpace(model.power_type)) { return GetResult(false, "用电类别不能为空"); } if (string.IsNullOrWhiteSpace(model.city_type)) { return GetResult(false, "城乡类别不能为空"); } model.business_photo = obj.business_photo; model.house_photo = obj.house_photo; model.sfz_front = obj.sfz_front; model.sfz_back = obj.sfz_back; model.apply_book = obj.apply_book; model.operator_idcard = obj.operator_idcard; model.zj1 = obj.zj1; model.zj2 = obj.zj2; //营业照 var file_business_photo = GetFile("business_photo"); if (saveFile(Dir, file_business_photo, Tool.GetNextId(id, 1), out string path)) { model.business_photo = path; } //房产证 var file_house_photo = GetFile("house_photo"); if (saveFile(Dir, file_house_photo, Tool.GetNextId(id, 2), out string path_house_photo)) { model.house_photo = path_house_photo; } //身份证正面 var file_sfz_front = GetFile("sfz_front"); if (saveFile(Dir, file_sfz_front, Tool.GetNextId(id, 3), out string path_sfz_front)) { model.sfz_front = path_sfz_front; } //身份证反面 var file_sfz_back = GetFile("sfz_back"); if (saveFile(Dir, file_sfz_back, Tool.GetNextId(id, 4), out string path_sfz_back)) { model.sfz_back = path_sfz_back; } //授权委托书 var file_apply_book = GetFile("apply_book"); if (saveFile(Dir, file_apply_book, Tool.GetNextId(id, 5), out string path_apply_book)) { model.apply_book = path_apply_book; } //经办人身份证 var file_operator_idcard = GetFile("operator_idcard"); if (saveFile(Dir, file_operator_idcard, Tool.GetNextId(id, 6), out string path_operator_idcard)) { model.operator_idcard = path_operator_idcard; } //其它证件1 var file_zj1 = GetFile("zj1"); if (saveFile(Dir, file_zj1, Tool.GetNextId(model.id, 7), out string path_zj1)) { model.zj1 = path_zj1; } //其它证件2 var file_zj2 = GetFile("zj2"); if (saveFile(Dir, file_zj2, Tool.GetNextId(model.id, 8), out string path_zj2)) { model.zj2 = path_zj2; } model.id = id; if (bll.Update(model)) { return GetResult(true, model, ""); } else { return GetResult(false, "修改失败"); } } else { return GetResult(false, "请求参数不能为空"); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return GetResult(false, "发生错误,请联系管理员"); } } //[Authorize] /// /// 查询所有联系人信息 /// [Route("All")] public JsonResult QueryALL() { var list = bll.GetModelList(""); var result = GetResult(true, list); return result; } //[Authorize] /// /// 查询单个客户收资 /// [Route("One")] public JsonResult One() { JsonResult ret; var id = GetValue("id"); if (string.IsNullOrEmpty(id)) { ret = GetResult(false, "id不能为空"); return ret; } var model = bll.GetModel(id); if (null== model) { ret = GetResult(false, "客户收资信息不存在 id:"+ id); return ret; } return GetResult(true, model,""); } #region 添加或修改客户地址 //[Authorize] [HttpPost] [Route("UpdateAddress")] public JsonResult UpdateAddress() { try { JsonResult ret; var client_id = GetValue("client_id"); if (string.IsNullOrEmpty(client_id)) { ret = GetResult(false, "client_id不能为空"); return ret; } var address = GetValue("address"); if (string.IsNullOrEmpty(address)) { ret = GetResult(false, "address不能为空"); return ret; } var model = bll_address.GetModelList("client_id='" + client_id + "'").OrderByDescending(s=>s.create_time).FirstOrDefault(); if (null== model) { model = new Competition.Mysql.Model.app_struct_address(); model.id = Tool.GetId(); model.create_time = DateTime.Now; model.client_id = client_id; model.address = address; if (bll_address.Add(model)) { return GetResult(true, model, ""); } else { return GetResult(false, "添加失败"); } } else { model.client_id = client_id; model.address = address; if (bll_address.Update(model)) { return GetResult(true, model, ""); } else { return GetResult(false, "修改失败"); } } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return GetResult(false, "发生错误,请联系管理员"); } } //[Authorize] [Route("QueryAddress")] public JsonResult QueryAddress() { try { JsonResult ret; var client_id = GetValue("client_id"); if (string.IsNullOrEmpty(client_id)) { ret = GetResult(false, "client_id不能为空"); return ret; } var model = bll_address.GetModelList("client_id='" + client_id + "'").OrderByDescending(s => s.create_time).FirstOrDefault(); if (null == model) { return GetResult(false, "记录不存在!"); } else { return GetResult(true, model, ""); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return GetResult(false, "发生错误,请联系管理员"); } } #endregion /// /// 修改申请容量 /// /// //[Authorize] [HttpPost] [Route("UpdateApplyCapacity")] public JsonResult UpdateApplyCapacity() { try { JsonResult ret; var client_id = GetValue("client_id"); if (string.IsNullOrEmpty(client_id)) { ret = GetResult(false, "client_id不能为空"); return ret; } var apply_capacity = GetValue("apply_capacity"); if (string.IsNullOrEmpty(apply_capacity)) { ret = GetResult(false, "apply_capacity不能为空"); return ret; } var model = bll.GetModel(client_id); if (null == model) { return GetResult(false, "客户收资记录不存在,client_id:"+ client_id); } else { model.r1 = apply_capacity; if (bll.Update(model)) { return GetResult(true, model, ""); } else { return GetResult(false, "修改失败"); } } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return GetResult(false, "发生错误,请联系管理员"); } } } }