ShippingTemplateDao.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @package merchant
  4. * Author: Qinii
  5. * Date: 2020/5/6
  6. * Time: 14:21
  7. */
  8. namespace app\common\dao\store\shipping;
  9. use app\common\model\store\shipping\ShippingTemplate;
  10. use app\entity\data\shipping\ShippingTemplateEntity;
  11. use think\facade\Db;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\shipping\ShippingTemplate as model;
  14. class ShippingTemplateDao extends BaseDao
  15. {
  16. /**
  17. * @Author:Qinii
  18. * @Date: 2020/5/8
  19. * @return string
  20. */
  21. protected function getModel(): string
  22. {
  23. return model::class;
  24. }
  25. /**
  26. * @Author:Qinii
  27. * @Date: 2020/5/7
  28. * @param int $merId
  29. * @param array $where
  30. * @return mixed
  31. */
  32. public function search(int $merId,array $where)
  33. {
  34. $query = ($this->getModel()::getDB())->where('mer_id',$merId)->order('sort desc');
  35. if(isset($where['name']) && !empty($where['name']))
  36. $query->where('name','like','%'.$where['name'].'%');
  37. if(isset($where['type']) && !empty($where['type']))
  38. $query->where('type',$where['type']);
  39. return $query;
  40. }
  41. /**
  42. * 查询是否存在
  43. * @Author:Qinii
  44. * @Date: 2020/5/7
  45. * @param int $merId
  46. * @param $field
  47. * @param $value
  48. * @param null $except
  49. * @return bool
  50. */
  51. public function merFieldExists(int $merId, $field, $value, $except = null)
  52. {
  53. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  54. $query->where($field, '<>', $except);
  55. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  56. }
  57. /**
  58. * 关联删除
  59. * @Author:Qinii
  60. * @Date: 2020/5/7
  61. * @param int $id
  62. * @return int|void
  63. */
  64. public function delete(int $id)
  65. {
  66. $result = $this->getModel()::with(['free','region','undelives'])->find($id);
  67. $result->together(['free','region','undelives'])->delete();
  68. }
  69. /**
  70. * 批量删除
  71. * @Author:Qinii
  72. * @Date: 2020/5/8
  73. * @param int $id
  74. * @return mixed
  75. */
  76. public function batchRemove(int $id)
  77. {
  78. return ($this->getModel())::getDB()->where($this->getPk(),'in',$id)->delete();
  79. }
  80. public function getList($merId)
  81. {
  82. // return ($this->getModel())::getDB()->where('mer_id',$merId)->field('shipping_template_id,name')->select();
  83. $list = ($this->getModel())::getDB()->where('mer_id',$merId)->field('shipping_template_id,name')->select()->toArray();
  84. $default = ['shipping_template_id' => 105, 'name' => '默认包邮'];//默认
  85. array_unshift($list, $default);
  86. return $list;
  87. }
  88. /**
  89. * @param int $shippingTemplateId
  90. * @return \app\entity\CommonEntity
  91. */
  92. public function getShippingTemplateEntityByShippingTemplateId(int $shippingTemplateId): \app\entity\CommonEntity
  93. {
  94. $data = [];
  95. try {
  96. /** @var ShippingTemplate $model */
  97. $model = $this->getModel();
  98. $dataRes = $model::getDB()
  99. ->where('shipping_template_id', '=', $shippingTemplateId)
  100. ->find();
  101. if (!empty($dataRes)) {
  102. $data = $dataRes->toArray();
  103. }
  104. } catch (\Exception $e) {
  105. }
  106. return ShippingTemplateEntity::newInstance($data);
  107. }
  108. }