123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package dao
- import (
- "demo/internal/model"
- "github.com/pkg/errors"
- "github.com/siddontang/go/log"
- )
- func (d *Dao) GetSimList(index, pagesize int64, card, company string, status int64) ([]*model.ListSim, int64, error) {
- rows := d.db.Table("sim").Alias("s")
- rows.Join("left", []string{"t_sb_device_connection", "r"}, "r.simid = s.id")
- rows.Join("left", []string{"t_nz_farm", "p"}, "r.FARM_UUID = p.UUID")
- rows.Join("left", []string{"t_jc_housephy", "h"}, "r.PHYHOUSE_UUID=h.UUID")
- rows.Select("s.*,r.IMEI, p.FARM_NAME,h.PHYSICALNAME as bname,p.FARM_NUM")
- defer rows.Close()
- if index != 0 && pagesize != 0 {
- first := (index - 1) * pagesize
- rows.Limit(int(pagesize), int(first))
- }
- if card != "" {
- rows.Where("s.card = ? ", card)
- }
- if company != "" {
- rows.Where("s.company = ? ", company)
- }
- if status != -1 {
- rows.Where("s.status = ? ", status)
- }
- dataList := make([]*model.ListSim, 0)
- count, err := rows.Desc("s.purchasedate").FindAndCount(&dataList)
- if err != nil {
- err = errors.Wrap(err, "GetSimList-db-error")
- log.Error(err)
- return dataList, 0, err
- }
- return dataList, count, nil
- }
- func (d *Dao) AddSim(arg model.Sim) (int64, error) {
- _, err := d.db.Insert(&arg)
- if err != nil {
- err = errors.Wrap(err, "AddSim-db-error")
- log.Error(err)
- return 0, err
- }
- return arg.Id, nil
- }
- func (d *Dao) EditSim(arg model.Sim) error {
- _, err := d.db.Where("id = ?", arg.Id).
- Cols("card,totalflow,used,remaining,company,purchasedate,activatedate,topupdate,enddate,status,receiverid,pastureid,barid,barname ").Update(&arg)
- if err != nil {
- err = errors.Wrap(err, "EditSim-db-error")
- log.Error(err)
- return err
- }
- return nil
- }
- func (d *Dao) GetSimPull() ([]*model.Sim, error) {
- dataList := make([]*model.Sim, 0)
- err := d.db.Select("id,card").Where("status = 1 ").
- Where(" (select count(1) from t_sb_device_connection where sim.id = t_sb_device_connection.simid ) = 0 ").Find(&dataList)
- if err != nil {
- err = errors.Wrap(err, "GetSimPull-db-error")
- log.Error(err)
- return dataList, err
- }
- return dataList, nil
- }
- func (d *Dao) EditSimReceiverid(arg model.Sim) error {
- _, err := d.db.Where("id = ?", arg.Id).
- Cols("receiverid ").Update(&arg)
- if err != nil {
- err = errors.Wrap(err, "EditSim-db-error")
- log.Error(err)
- return err
- }
- return nil
- }
- func (d *Dao) CheckSimCode(id int64, card string) (bool, error) {
- rows := d.db.NewSession().Table(new(model.Sim).TableName())
- rows.Close()
- if id != 0 {
- rows.Where("id != ? ", id)
- }
- return rows.Where("card = ? ", card).Exist()
- }
|