日常中我们经常需要用XLS表进行操作,或是自动批量录入,现在开始以手机号卡为例进行测试。

分析号卡表

  • 手机卡号有19位,比如8986032094857889550,然后一次发放给某人250张,则是8986032094857889551,8986032094857889552,… ,8986032094857889749.
  • 我们管理的时候,应该按日期进行管理,具体为以下字段

image.png

  • 个人觉得这个魔方的后台管理表格功能也很方便的,我便继续使用拉,用以前的步骤开始建表和模
  • 关于19位的卡号,我想来想去,因为前面部份一样,基本后四位有变化,我就分成四段,前五位,再五位,再五位,最后四位这样录入,然后存为sqllite的TEXT字段类型算了

    建实体

    在我的Areas/Business/Models下的 下的Model.xml进行新的表的创建,我为了后续方便,我多写了几个备用可以为空的字段 ```


  1. - 保存
  2. - 重新生成解决方案
  3. - 然后我们在build_netcore.tt上点键,运行自定义工具,点确定,但我上次生成二个表后,又动了一些,现在生成出错,为了我这个最主要的管理,我现在只留上面这个表,保存,生成方案,还是出错。
  4. - 然后我郁闷了,重新新建项目,安装魔方包,然后将models弄过去,只留 了这一个
  5. - **上面有教程就不再写了,只写这里生成实体之后的**
  6. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616819464374-a7da586a-ff80-4c38-b113-d234d3aa7dae.png#align=left&display=inline&height=219&margin=%5Bobject%20Object%5D&name=image.png&originHeight=219&originWidth=426&size=13831&status=done&style=none&width=426)<br />然后增加区域等,正常显示这些东东拉。<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616820141590-0ea49ae0-c5d4-4d34-ac1f-f7b939fc70a0.png#align=left&display=inline&height=280&margin=%5Bobject%20Object%5D&name=image.png&originHeight=280&originWidth=1249&size=30750&status=done&style=none&width=1249)
  7. <a name="8JeL2"></a>
  8. ## 核实角色权限
  9. - 核实一下是否高级用户,我设计的是高级用户的角色可以新建管理这个表,其他人只能看。
  10. - 在我的页面中AboutMe中,看是否是高级用户,如是,则显示菜单
@if (@ViewBag.RoleName == “高级用户”) { 发卡 }
其实这一步也没必要这样作,呵, <a name="JnHuF"></a> ## 新批量新建卡发放 <a name="Wr3de"></a> ### 建菜单和视图 在我们新建的域中的卡的控制器中,写入新增方法 ADDUimCard,当然有GET和POST<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616826204542-904c0ec4-6503-41c1-96c8-4bb8da0cfdac.png#align=left&display=inline&height=193&margin=%5Bobject%20Object%5D&name=image.png&originHeight=193&originWidth=440&size=12901&status=done&style=none&width=440) - 写好后,先生成视图,会发现,他在Business下面新出现了一个VIews文件夹,这个视频是在这个下面生成的,而不是根目录下的VIEWS下面,然后你也会发现,他没有使用头部和尾部的的css的布局页默认页。 public IActionResult ADDUimCard( ) { return View(); } [HttpPost] [ValidateAntiForgeryToken] public IActionResult ADDUimCard(UimCard uimcard) { if (ModelState.IsValid) { //var course1 = new Course //{ // CourseName = course.CourseName, // CteatTime = course.CteatTime //}; //course1.Insert(); //return RedirectToAction(nameof(Index)); } return View(); } - 暂时没有写代码。 - 我们在后台新建菜单 ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616827490197-b5c2f367-5ab5-46ea-96a1-7d1ab9845860.png#align=left&display=inline&height=114&margin=%5Bobject%20Object%5D&name=image.png&originHeight=114&originWidth=1006&size=13636&status=done&style=none&width=1006) - 然后 ![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616827502602-8e6d0976-f2fa-4367-b7b1-7dcfe1c049dd.png#align=left&display=inline&height=130&margin=%5Bobject%20Object%5D&name=image.png&originHeight=130&originWidth=479&size=10053&status=done&style=none&width=479) - 设计前台 @ For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @ @{ Layout = “_Layout1”; }

UIM卡的发放登记


@Html.AntiForgeryToken()

    </div>




</div>
<div class="form-group">
    <label for="exampleInputEmail1">领取人</label>
    <input type="email" class="form-control" name="Receiver" placeholder="领取人">
</div>




<button type="submit" class="btn btn-default">提交</button>


- 效果

![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616829117650-aa135beb-4204-4d2e-8401-146057d402a1.png#align=left&display=inline&height=539&margin=%5Bobject%20Object%5D&name=image.png&originHeight=539&originWidth=1157&size=31177&status=done&style=none&width=1157)

- 后台代码

[HttpGet] public IActionResult ADDUimCard( ) { return View(); }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult ADDUimCard(string StratId, string EndId, string Number, string Region, string Receiver, string Bei1)
    {
        string fullfirstcard = StratId + EndId;
        if (fullfirstcard.Length != 19) {
            return RedirectToAction("Error", "UimCard", new { ErrorText = "卡数位数输入错误,请重来" });
        }

        if (Convert.ToInt32(Number)> 250)
        {
            return RedirectToAction("Error", "UimCard", new { ErrorText = "一次性发卡超过250张拉" });
        }

        ///检测第一个和最后一个卡号是否在系统存在

        int endcard = Convert.ToInt32(EndId) + Convert.ToInt32(Number)-1;
        string fullendtcard = StratId + endcard.ToString();
        var mm = UimCard.FindAll(UimCard._.CardId.Contains(fullfirstcard));

        var nn = UimCard.FindAll(UimCard._.CardId.Contains(fullendtcard));
        if(mm.Count!=0 ||nn.Count!=0)
        {
            return RedirectToAction("Error", "UimCard", new { ErrorText = "该卡号已存在拉!已发放过拉!" });
        }
        //准备对发放的卡进行录入系统,用卡的张数进行foreach
        int cardnumber = Convert.ToInt32(Number);
        int endcardnum = Convert.ToInt32(EndId);
        for (int i = 0; i < cardnumber; i++)
        {
            var uimcard = new UimCard
            {
                CardId = StratId + endcardnum.ToString(),
                County = "金沙",
                Region = Region,
                BPSName = Receiver,
                Receiver = Receiver,
                CardState = "0",Bei1= Bei1

            };
            uimcard.Insert();
            endcardnum++;

        }

        return RedirectToAction("Error", "UimCard", new { ErrorText = "已录入成功拉!" });


    }


public IActionResult Error(string ErrorText) { ViewBag.errortext = ErrorText; return View(); }


- 在测试魔方后台导出的时候,因为卡号19位,长了,导出来后5位全是00000,所以不得行,我只能通过在前后加个字符A来解决

![image.png](https://cdn.nlark.com/yuque/0/2021/png/1174002/1616845903671-c5aee22a-6395-4be8-9fc6-458c14ec33e6.png#align=left&display=inline&height=118&margin=%5Bobject%20Object%5D&name=image.png&originHeight=118&originWidth=409&size=8469&status=done&style=none&width=409)

- 在后台改代码<br />

CardId =”A”+ StratId + endcardnum.ToString()+ “A”, ``` 最后完成
image.png