TagGood.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class TagGood extends BaseController{
  7. public function __construct(App $app) {parent::__construct($app);}
  8. //新建标签
  9. public function create(){
  10. $post = $this->post;
  11. $type = isset($post['type'])&& $post['type']!="" ? intval($post['type']) :"";
  12. if($type==''){
  13. return error_show(1004,"参数 type 不能未空");
  14. }
  15. $tagName =isset($post['tag_name'])&&$post['tag_name']!='' ? trim($post['tag_name']):"";
  16. if($tagName==''){
  17. return error_show(1004,"参数 tag_name 不能未空");
  18. }
  19. $isT =Db::name("order_tag")->where(["type"=>$type,"tag_name"=>$tagName,"is_del"=>0])->findOrEmpty();
  20. if(!empty($isT)){
  21. return error_show(1004,"标签名称已存在");
  22. }
  23. $tagData=[
  24. "type"=>$type,
  25. "tag_name"=>$tagName,
  26. "status"=>1,
  27. "apply_id"=>$this->uid,
  28. "apply_name"=>$this->uname,
  29. "addtime"=>date("Y-m-d H:i:s"),
  30. "updatetime"=>date("Y-m-d H:i:s")
  31. ];
  32. $ins=Db::name("order_tag")->insert($tagData);
  33. return $ins?app_show(0,"标签新建成功"): error_show(1004,"标签新建失败");
  34. }
  35. //标签列表翻页
  36. public function list(){
  37. $page = isset($this->post['page'])&&$this->post['page']!=''?intval($this->post['page']):1;
  38. $size = isset($this->post['size'])&&$this->post['size']!=''?intval($this->post['size']):15;
  39. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):'';
  40. $status = isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):'';
  41. $condition =[["is_del","=",0]];
  42. if($type!=''){
  43. $condition[]=["type","=",$type];
  44. }
  45. if($status!==''){
  46. $condition[]=["status","=",$status];
  47. }
  48. $tagName =isset($this->post['tag_name'])&&$this->post['tag_name']!='' ? trim($this->post['tag_name']):"";
  49. if($tagName!=''){
  50. $condition[]=["tag_name","like","%$tagName%"];
  51. }
  52. $count =Db::name("order_tag")->where($condition)->count();
  53. $total=ceil($count/$size);
  54. $page = $page>=$total ? intval($total):$page;
  55. $list =Db::name("order_tag")->where($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  56. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  57. }
  58. //根据条件筛选合适的标签数据
  59. public function query(){
  60. $type = isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):'';
  61. $condition =[["is_del","=",0]];
  62. if($type!=''){
  63. $condition[]=["type","=",$type];
  64. }
  65. $tagName =isset($this->post['tag_name'])&&$this->post['tag_name']!='' ? trim($this->post['tag_name']):"";
  66. if($tagName!=''){
  67. $condition[]=["tag_name","like","%$tagName%"];
  68. }
  69. $status = isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):'';
  70. if($status!==''){
  71. $condition[]=["status","=",$status];
  72. }
  73. $list =Db::name("order_tag")->where($condition)->order("addtime desc")->select()->toArray();
  74. return app_show(0,"获取成功",$list);
  75. }
  76. //启禁用状态
  77. public function status(){
  78. $id =isset($this->post['id'])&&$this->post['id']!=''?intval($this->post['id']):'';
  79. if($id==''){
  80. return error_show(1004,"参数 id 不能为空");
  81. }
  82. $taginfo =Db::name("order_tag")->find(["id"=>$id]);
  83. if($taginfo==false){
  84. return error_show(1004,"标签数据不存在");
  85. }
  86. $status=isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):'';
  87. if($status===''){
  88. return error_show(1004,"参数 status 不能为空");
  89. }
  90. $update =["status"=>$status,"updatetime"=>date("Y-m-d H:i:s")];
  91. $up =Db::name("order_tag")->where($taginfo)->update($update);
  92. return $up?app_show(0,"标签更新成功"): error_show(1004,"标签更新失败");
  93. }
  94. //编辑标签名称类型数据
  95. public function save(){
  96. $id =isset($this->post['id'])&&$this->post['id']!=''?intval($this->post['id']):'';
  97. if($id==''){
  98. return error_show(1004,"参数 id 不能为空");
  99. }
  100. $taginfo =Db::name("order_tag")->where(["id"=>$id,"is_del"=>0])->find();
  101. if($taginfo==false){
  102. return error_show(1004,"标签数据不存在");
  103. }
  104. $tagName=isset($this->post['tag_name'])&&$this->post['tag_name']!=''?trim($this->post['tag_name']):'';
  105. if($tagName===''){
  106. return error_show(1004,"参数 tag_name 不能为空");
  107. }
  108. $type=isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):'';
  109. if($type==''){
  110. return error_show(1004,"参数 type 不能为空");
  111. }
  112. $tag =Db::name("order_tag")->where([['tag_name',"=",$tagName],['type',"=",$type],["id","<>",$id],["is_del","=",0]])->find();
  113. if($tag!=false){
  114. return error_show(1004,"标签名称已存在");
  115. }
  116. $update =["type"=>$type,"tag_name"=>$tagName,"updatetime"=>date("Y-m-d H:i:s")];
  117. $up =Db::name("order_tag")->where($taginfo)->update($update);
  118. return $up?app_show(0,"标签更新成功"): error_show(1004,"标签更新失败");
  119. }
  120. //删除标签
  121. public function delete(){
  122. $id =isset($this->post['id'])&&$this->post['id']!=''?intval($this->post['id']):'';
  123. if($id==''){
  124. return error_show(1004,"参数 id 不能为空");
  125. }
  126. $taginfo =Db::name("order_tag")->find(["id"=>$id]);
  127. if($taginfo==false){
  128. return error_show(1004,"标签数据不存在");
  129. }
  130. $update=["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  131. $up =Db::name("order_tag")->where($taginfo)->update($update);
  132. return $up?app_show(0,"标签删除成功"): error_show(1004,"标签删除失败");
  133. }
  134. //单号添加标签
  135. public function AddTag(){
  136. $tagid =isset($this->post["tag_id"]) && $this->post["tag_id"]!=""?intval($this->post["tag_id"]) :"";
  137. if($tagid==""){
  138. return error_show(1004,"参数 tag_id 不能为空");
  139. }
  140. $taginfo =Db::name("order_tag")->where(["id"=>$tagid,"is_del"=>0])->find();
  141. if($taginfo==false){
  142. return error_show(1005,"标签数据不存在");
  143. }
  144. if($taginfo['status']==0){
  145. return error_show(1005,"标签已禁用");
  146. }
  147. $code =isset($this->post['code']) &&$this->post["code"]!="" ? trim($this->post["code"]):"";
  148. if($code==''){
  149. return error_show(1004,"参数 code 不能为空");
  150. }
  151. $taglog =Db::name("tag_log")->where(["tag_id"=>$tagid,"code"=>$code,"status"=>1])->findOrEmpty();
  152. $total_fee =isset($this->post['total_fee']) &&$this->post["total_fee"]!=="" ? floatval($this->post["total_fee"]):"";
  153. if($total_fee==''){
  154. return error_show(1004,"参数 total_fee 不能为空");
  155. }
  156. Db::startTrans();
  157. try{
  158. if(!empty($taglog)){
  159. $up = Db::name("tag_log")->where($taglog)->update(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  160. if($up==false){
  161. Db::rollback();
  162. return error_show(1004,"标签日志新建失败");
  163. }
  164. }
  165. if($taginfo['type']==1 || $taginfo['type']==2){
  166. $result= $this->payTag($taginfo['type'],$code,$total_fee);
  167. }
  168. if($taginfo['type']==3|| $taginfo['type']==4){
  169. $result= $this->qrdTag($taginfo['type'],$code,$total_fee);
  170. }
  171. $tagdata=[
  172. "code"=>$code,
  173. "tag_id"=>$tagid,
  174. "creater"=>$this->uname,
  175. "createrid"=>$this->uid,
  176. "tag_fee"=>$total_fee,
  177. "status"=>1,
  178. "addtime"=>date("Y-m-d H:i:s"),
  179. "updatetime"=>date("Y-m-d H:i:s")
  180. ];
  181. $in =Db::name("tag_log")->insert($tagdata);
  182. if($in==false){
  183. Db::rollback();
  184. return error_show(1004,"标签日志新建失败");
  185. }
  186. $data =$result->getData();
  187. if($data['code']==0){
  188. Db::commit();
  189. return app_show(0,"标签添加成功");
  190. }
  191. Db::rollback();
  192. return error_show($data['code'],$data['message']);
  193. }catch (\Exception $e){
  194. Db::rollback();
  195. return error_show(1004,$e->getMessage());
  196. }
  197. }
  198. /**
  199. * @param $tagId 标签类型 1 付款2回票
  200. * @param $code 对账单编号
  201. * @param $total_fee 标签金额
  202. * @return \think\response\Json|void
  203. * @throws \think\db\exception\DbException
  204. */
  205. private function payTag($tagId,$code,$total_fee){
  206. $pay=Db::name("pay")->where(["payNo"=>$code,"is_del"=>0])->findOrEmpty();
  207. if(empty($pay)){
  208. return error_show(1004,"未找到对账单数据");
  209. }
  210. $update=["updatetime"=>date("Y-m-d H:i:s")];
  211. if($tagId==1){
  212. if($pay['wpay_fee']+$pay['pay_tag_fee']<$total_fee){
  213. return error_show(1004,"对账单未付金额不足");
  214. }
  215. $update['wpay_fee']=$pay['wpay_fee']+$pay['pay_tag_fee']-$total_fee;
  216. $status = $update['wpay_fee']==0 &&$pay['pay_fee']==0 ? 3:($pay['apay_fee']==0?1:2);
  217. $update['pay_tag_fee'] = $total_fee;
  218. $update['pay_status'] = $status;
  219. $update['pay_tag'] = 1;
  220. }
  221. if($tagId==2){
  222. if($pay['winv_fee']+$pay['inv_tag_fee']<$total_fee){
  223. return error_show(1004,"对账单未付金额不足");
  224. }
  225. $update['winv_fee']=$pay['winv_fee']+$pay['inv_tag_fee']-$total_fee;
  226. $status = $update['winv_fee']==0 &&$pay['inv_fee']==0 ? 3:($pay['ainv_fee']==0?1:2);
  227. $update['inv_tag_fee'] = $total_fee;
  228. $update['inv_status'] = $status;
  229. $update['inv_tag'] = 1;
  230. }
  231. $resulr= Db::name("pay")->where($pay)->update($update);
  232. return $resulr? app_show(0,"标签添加成功"):error_show(1004,"标签添加失败");
  233. }
  234. /**
  235. * @param $tagId 标签类型 3 回款4开票
  236. * @param $code 销售单编号
  237. * @param $total_fee 标签金额
  238. * @return \think\response\Json|void
  239. * @throws \think\db\exception\DbException
  240. */
  241. private function qrdTag($tagId,$code,$total_fee){
  242. $qrd=Db::name("qrd_info")->where(["sequenceNo"=>$code,"is_del"=>0])->findOrEmpty();
  243. if(empty($qrd)){
  244. return error_show(1004,"未找到销售单数据");
  245. }
  246. $update=["updatetime"=>date("Y-m-d H:i:s")];
  247. if($tagId==3){
  248. if($qrd['wpay_fee']+$qrd['pay_tag_fee']<$total_fee){
  249. return error_show(1004,"对账单未付金额不足");
  250. }
  251. $update['wpay_fee']=$qrd['wpay_fee']+$qrd['pay_tag_fee']-$total_fee;
  252. $status = $update['wpay_fee']==0 &&$qrd['pay_fee']==0 ? 3:($qrd['apay_fee']==0?1:2);
  253. $update['pay_tag_fee'] = $total_fee;
  254. $update['pay_status'] = $status;
  255. $update['pay_tag'] = 1;
  256. }
  257. if($tagId==4){
  258. if($qrd['winv_fee']+$qrd['inv_tag_fee']<$total_fee){
  259. return error_show(1004,"对账单未付金额不足");
  260. }
  261. $update['winv_fee']=$qrd['winv_fee']+$qrd['inv_tag_fee']-$total_fee;
  262. $status = $update['winv_fee']==0 &&$qrd['inv_fee']==0 ? 3:($qrd['ainv_fee']==0?1:2);
  263. $update['inv_tag_fee'] = $total_fee;
  264. $update['inv_status'] = $status;
  265. $update['inv_tag'] = 1;
  266. }
  267. $resulr= Db::name("qrd_info")->where($qrd)->update($update);
  268. return $resulr? app_show(0,"标签添加成功"):error_show(1004,"标签添加失败");
  269. }
  270. }