work.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:work.py 农事相关
  6. author:rabin
  7. """
  8. from __load__ import *
  9. class index_path(Load):
  10. """
  11. @api {get} /work 获取农事记录列表
  12. @apiVersion 1.0.0
  13. @apiName getWorkList
  14. @apiGroup Farm
  15. @apiParam {String} page 页数
  16. @apiSuccess {Object[]} setting 基本信息
  17. @apiSuccess {String} setting.name 站点名
  18. @apiSuccess {Number} setting.farm 当前的农场ID
  19. @apiSuccess {Object[]} list 农事列表信息
  20. @apiSuccess {String} list.name 名称
  21. @apiSuccess {String} list.id ID
  22. @apiSuccess {Object[]} page 分页信息
  23. @apiSuccess {String} page.current 当前页
  24. @apiSuccess {String} page.total 总页数
  25. @apiSuccess {String} page.num 每页数据条数
  26. @apiSuccess {String} page.totalNum 总数据条数
  27. @apiErrorExample 操作成功
  28. {"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"category": "\u7528\u836f", "workdates": "2017-10-11", "pic": "http://www.xinnongbaohe.com:8087/upload/2017/10/10/36083979c0984547320137e5f01f7e52.jpg", "name": "2017-10-11 - \u5730\u57572\u53f7/\u7528\u836f", "land_id": "6b18f8b5-c9e5-5600-8808-dad30fa4ea93", "farm_id": 1, "state": true, "content": "22222", "amount": "30L222", "workdate": 1507680000, "category_id": "fcd8899a-a3a5-52ee-a2a8-2d5c30160044", "cdate": 1509347365, "id": "ee57691d-d4e7-533c-9111-3fc059120a7b", "land": "\u5730\u57572\u53f7"}], "page": {"current": 1, "ajax": false, "total": 1, "num": 15, "totalNum": 1}, "base": {}}}
  29. @apiErrorExample 操作失败
  30. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  31. """
  32. @auth
  33. @Web.setting
  34. def get(self):
  35. self.data['list'] = self.service('common').list('farm_work', state=True, search={'farm_id': Demeter.config['setting']['farm']})
  36. if self.data['list']:
  37. for key, value in enumerate(self.data['list']):
  38. self.data['list'][key]['workdates'] = Demeter.date(value['workdate'], '%Y-%m-%d')
  39. category = self.service('common').one('farm_work_category', id=value['category_id'])
  40. self.data['list'][key]['category'] = category['name']
  41. land = self.service('common').one('farm_work_land', id=value['land_id'])
  42. if land:
  43. self.data['list'][key]['land'] = land['name']
  44. self.data['list'][key]['name'] = self.data['list'][key]['workdates'] + ' / ' + land['name'] + ' - ' + category['name']
  45. else:
  46. self.data['list'][key]['name'] = self.data['list'][key]['workdates'] + ' / ' + category['name']
  47. self.out('yes', self.data)
  48. """
  49. @api {post} /work 更新农事记录
  50. @apiVersion 1.0.0
  51. @apiName updateWork
  52. @apiGroup Farm
  53. @apiParam {String} id 农事ID,为空则插入新记录
  54. @apiParam {String} update_pic 图片,多个逗号隔开
  55. @apiParam {String} update_category 农事方式ID
  56. @apiParam {String} update_land 地块ID
  57. @apiParam {String} update_workdate 农事时间:2017-10-10
  58. @apiParam {String} update_content 劳作内容
  59. @apiParam {String} update_amount 劳作数量
  60. @apiSuccess {Bool} status 状态
  61. @apiSuccess {String} msg 描述
  62. @apiSuccess {Number} code 状态码
  63. @apiErrorExample 操作成功
  64. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  65. @apiErrorExample 操作失败
  66. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  67. """
  68. @auth
  69. @Web.setting
  70. def post(self):
  71. id = self.input('id', None)
  72. update = {}
  73. update['pic'] = self.input('update_pic')
  74. update['category_id'] = self.input('update_category')
  75. update['land_id'] = self.input('update_land')
  76. update['workdate'] = self.input('update_workdate')
  77. update['content'] = self.input('update_content')
  78. update['amount'] = self.input('update_amount')
  79. if not update['category_id']:
  80. self.out(u'请选择农事方式')
  81. return
  82. if not update['land_id']:
  83. self.out(u'请选择地块')
  84. return
  85. if not update['workdate']:
  86. self.out(u'请输入农事时间')
  87. return
  88. if not update['content']:
  89. self.out(u'请输入劳作内容')
  90. return
  91. if not update['amount']:
  92. self.out(u'请输入劳作数量')
  93. return
  94. #update['workdate'] = Demeter.mktime(update['workdate'], '%Y-%m-%d')
  95. state = self.service('common').update('farm_work', id, update, cdate=False)
  96. self.out('yes', {'id':state})
  97. class info_path(Load):
  98. """
  99. @api {get} /work/info 获取农事信息
  100. @apiVersion 1.0.0
  101. @apiName getWorkInfo
  102. @apiGroup Farm
  103. @apiParam {String} id 农事ID
  104. @apiSuccess {Object[]} setting 基本信息
  105. @apiSuccess {String} setting.name 站点名
  106. @apiSuccess {Number} setting.farm 当前的农场ID
  107. @apiSuccess {Object[]} info 农事信息
  108. @apiSuccess {String} info.name 名称
  109. @apiSuccess {String} info.id ID
  110. @apiErrorExample 操作成功
  111. {"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"pic": "http://192.168.15.10:8087/upload/2017/08/30/455f9d6c26b5c989b32eda1809e58d5f.jpg", "method_id": null, "land_id": "249be20d-5759-5af0-9859-b770d9f44e2a", "farm_id": 1, "state": true, "amount": "100ml", "workdate": 1504105397, "category_id": "9365ea66-be55-56f7-a511-059fc85a9478", "cdate": 1504076614, "id": "19e05f00-ae55-5605-be12-86dcd2866df9"}], "page": {"current": 1, "ajax": false, "total": 1.0, "num": 15, "totalNum": 1}}}
  112. @apiErrorExample 操作失败
  113. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  114. """
  115. @auth
  116. @Web.setting
  117. def get(self):
  118. id = self.input('id', None)
  119. if id:
  120. self.data['info'] = self.service('common').one('farm_work', id=id)
  121. if self.data['info']:
  122. self.data['info']['workdates'] = Demeter.date(self.data['info']['workdate'], '%Y-%m-%d')
  123. self.out('yes', self.data)
  124. else:
  125. self.out('no')
  126. class land_path(Load):
  127. """
  128. @api {get} /work/land 获取农事地块列表
  129. @apiVersion 1.0.0
  130. @apiName getWorkLandList
  131. @apiGroup Farm
  132. @apiSuccess {Object[]} setting 基本信息
  133. @apiSuccess {String} setting.name 站点名
  134. @apiSuccess {Number} setting.farm 当前的农场ID
  135. @apiSuccess {Object[]} list 地块列表信息
  136. @apiSuccess {String} list.name 名称
  137. @apiSuccess {String} list.id ID
  138. @apiErrorExample 操作成功
  139. {"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"state": true, "farm_id": 1, "cdate": 1509344166, "id": "6b18f8b5-c9e5-5600-8808-dad30fa4ea93", "name": "\u5730\u57572\u53f7"}, {"state": true, "farm_id": 1, "cdate": 1509344164, "id": "17b881d4-ea79-5968-8328-4d7587cf3e39", "name": "\u5730\u57571\u53f7"}], "base": {}}}
  140. @apiErrorExample 操作失败
  141. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  142. """
  143. @auth
  144. @Web.setting
  145. def get(self):
  146. self.data['list'] = self.service('common').list('farm_work_land', state=True, search={'farm_id': Demeter.config['setting']['farm']})
  147. self.out('yes', self.data)
  148. """
  149. @api {post} /work/land 更新地块
  150. @apiVersion 1.0.0
  151. @apiName updateWorkLand
  152. @apiGroup Farm
  153. @apiParam {String} id 地块ID,为空则插入新记录
  154. @apiParam {String} update_name 地块名称
  155. @apiSuccess {Bool} status 状态
  156. @apiSuccess {String} msg 描述
  157. @apiSuccess {Number} code 状态码
  158. @apiErrorExample 操作成功
  159. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  160. @apiErrorExample 操作失败
  161. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  162. """
  163. @auth
  164. @Web.setting
  165. def post(self):
  166. id = self.input('id', None)
  167. update = {}
  168. update['name'] = self.input('update_name')
  169. if not update['name']:
  170. self.out(u'请输入地块名称')
  171. return
  172. state = self.service('common').update('farm_work_land', id, update, cdate=False)
  173. self.out('yes', {'id':state})
  174. """
  175. @api {delete} /work/land 删除地块
  176. @apiVersion 1.0.0
  177. @apiName deleteWorkLand
  178. @apiGroup Farm
  179. @apiParam {String} id 地块ID,为空则插入新记录
  180. @apiSuccess {Bool} status 状态
  181. @apiSuccess {String} msg 描述
  182. @apiSuccess {Number} code 状态码
  183. @apiErrorExample 操作成功
  184. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  185. @apiErrorExample 操作失败
  186. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  187. """
  188. @auth
  189. @Web.setting
  190. def delete(self):
  191. id = self.input('id')
  192. if id:
  193. model = Demeter.model('farm_work_land')
  194. model.id = id
  195. model.delete()
  196. self.out('yes', {'id':id})
  197. class land_pic_path(Load):
  198. """
  199. @api {get} /work/land_pic 获取农事地块图片列表
  200. @apiVersion 1.0.0
  201. @apiName getWorkLandPicList
  202. @apiGroup Farm
  203. @apiSuccess {Object[]} setting 基本信息
  204. @apiSuccess {String} setting.name 站点名
  205. @apiSuccess {Number} setting.farm 当前的农场ID
  206. @apiSuccess {Object[]} list 地块列表信息
  207. @apiSuccess {String} list.name 名称
  208. @apiSuccess {String} list.id ID
  209. @apiSuccess {Object[]} list.pic 图片
  210. @apiErrorExample 操作成功
  211. {"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"farm_id": 1, "name": "222", "pic": [{"pic": "http://192.168.15.10:8088/upload/2017/11/01/a7164193b56a074d72e4f94e4a4d145c.jpg", "land_id": "d508f554-db86-52dc-ba20-fbb476c60abc", "farm_id": 1, "state": true, "cdate": 1509519631, "id": "0d11d5ee-990e-52d5-ae55-09a212834f0d"}], "cdate": 1509519537, "state": true, "id": "d508f554-db86-52dc-ba20-fbb476c60abc"}, {"farm_id": 1, "name": "111", "pic": [], "cdate": 1509519533, "state": true, "id": "03679a19-f619-55d3-959c-bbc0a22dc072"}], "base": {}}}
  212. @apiErrorExample 操作失败
  213. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  214. """
  215. @auth
  216. @Web.setting
  217. def get(self):
  218. self.data['list'] = self.service('common').list('farm_work_land', state=True, search={'farm_id': Demeter.config['setting']['farm']})
  219. #self.data['list'] = self.service('common').list('farm_work_land_pic', state=True, search={'farm_id': Demeter.config['setting']['farm']})
  220. if self.data['list']:
  221. for key, value in enumerate(self.data['list']):
  222. self.data['list'][key]['pic'] = self.service('common').list('farm_work_land_pic', search={'farm_id': Demeter.config['setting']['farm'], 'land_id':value['id']}, state=True)
  223. self.out('yes', self.data)
  224. """
  225. @api {post} /work/land_pic 更新地块图片
  226. @apiVersion 1.0.0
  227. @apiName updateWorkLandPic
  228. @apiGroup Farm
  229. @apiParam {String} update_land_id 地块ID,为空则插入新记录
  230. @apiParam {String} update_pic 地块图片地址
  231. @apiSuccess {Bool} status 状态
  232. @apiSuccess {String} msg 描述
  233. @apiSuccess {Number} code 状态码
  234. @apiErrorExample 操作成功
  235. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  236. @apiErrorExample 操作失败
  237. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  238. """
  239. @auth
  240. @Web.setting
  241. def post(self):
  242. id = self.input('id', None)
  243. update = {}
  244. update['land_id'] = self.input('update_land_id')
  245. update['pic'] = self.input('update_pic')
  246. if not update['land_id']:
  247. self.out(u'请选择地块')
  248. return
  249. if not update['pic']:
  250. self.out(u'请上传地块图片')
  251. return
  252. state = self.service('common').update('farm_work_land_pic', id, update, cdate=False)
  253. self.out('yes', {'id':state})
  254. """
  255. @api {delete} /work/land_pic 删除地块图片
  256. @apiVersion 1.0.0
  257. @apiName deleteWorkLandPic
  258. @apiGroup Farm
  259. @apiParam {String} id 图片ID
  260. @apiSuccess {Bool} status 状态
  261. @apiSuccess {String} msg 描述
  262. @apiSuccess {Number} code 状态码
  263. @apiErrorExample 操作成功
  264. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  265. @apiErrorExample 操作失败
  266. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  267. """
  268. @auth
  269. @Web.setting
  270. def delete(self):
  271. id = self.input('id')
  272. if id:
  273. model = Demeter.model('farm_work_land_pic')
  274. model.id = id
  275. model.delete()
  276. self.out('yes', {'id':id})
  277. class category_path(Load):
  278. """
  279. @api {get} /work/category 获取农事分类列表
  280. @apiVersion 1.0.0
  281. @apiName getWorkCateList
  282. @apiGroup Farm
  283. @apiSuccess {Object[]} setting 基本信息
  284. @apiSuccess {String} setting.name 站点名
  285. @apiSuccess {Number} setting.farm 当前的农场ID
  286. @apiSuccess {Object[]} list 分类列表信息
  287. @apiSuccess {String} list.name 名称
  288. @apiSuccess {String} list.id ID
  289. @apiErrorExample 操作成功
  290. {"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": "229cfd36-9399-5599-b2b5-c53174c3de19", "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"state": true, "farm_id": 1, "cdate": 1509344125, "id": "fcd8899a-a3a5-52ee-a2a8-2d5c30160044", "name": "\u7528\u836f"}, {"state": true, "farm_id": 1, "cdate": 1509344107, "id": "6f955928-9724-5cb7-acd0-a81d482d9883", "name": "\u65bd\u80a5"}], "base": {}}}
  291. @apiErrorExample 操作失败
  292. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  293. """
  294. @auth
  295. @Web.setting
  296. def get(self):
  297. self.data['list'] = self.service('common').list('farm_work_category', state=True, search={'farm_id': Demeter.config['setting']['farm']})
  298. self.out('yes', self.data)
  299. """
  300. @api {post} /work/category 更新农事分类
  301. @apiVersion 1.0.0
  302. @apiName updateWorkCategory
  303. @apiGroup Farm
  304. @apiParam {String} id 分类ID,为空则插入新记录
  305. @apiParam {String} update_name 分类名称
  306. @apiSuccess {Bool} status 状态
  307. @apiSuccess {String} msg 描述
  308. @apiSuccess {Number} code 状态码
  309. @apiErrorExample 操作成功
  310. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  311. @apiErrorExample 操作失败
  312. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  313. """
  314. @auth
  315. @Web.setting
  316. def post(self):
  317. id = self.input('id', None)
  318. update = {}
  319. update['name'] = self.input('update_name')
  320. if not update['name']:
  321. self.out(u'请输入分类名称')
  322. return
  323. state = self.service('common').update('farm_work_category', id, update, cdate=False)
  324. self.out('yes', {'id':state})