nutrient.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <div class="app-container">
  3. <div class="filter-container">
  4. 营养素:
  5. <el-select
  6. ref="nutrientSelect"
  7. v-model="params.nutrientId"
  8. filterable
  9. remote
  10. reserve-keyword
  11. :default-first-option="true"
  12. style="width: 200px;margin-left: 10px;"
  13. placeholder="请输入营养素关键词"
  14. :remote-method="queryNutrients"
  15. :loading="loading"
  16. @change="nutrientChanged"
  17. >
  18. <el-option v-for="item in nutrients" :key="item.id" :label="item.name" :value="item.id" />
  19. </el-select>
  20. 计量:
  21. <el-radio v-model="params.radio" :label="0" @change="radioChange(params)">值</el-radio>
  22. <el-radio v-model="params.radio" :label="1" @change="radioChange(params)">范围</el-radio>
  23. <el-radio v-model="params.radio" :label="2" @change="radioChange(params)">误差</el-radio>
  24. <div v-if="params.radio === 1" style="display: inline-block">
  25. 大于等于:
  26. <el-input v-model="params.quantityMin" style="width: 80px;" class="filter-item" />
  27. 小于等于:
  28. <el-input v-model="params.quantityMax" style="width: 80px;" class="filter-item" />
  29. </div>
  30. <div v-else-if="params.radio === 2" style="display: inline-block">
  31. 基准:
  32. <el-input v-model="params.quantity" style="width: 80px;" class="filter-item" />
  33. ±:
  34. <el-input v-model="params.stdError" style="width: 80px;" class="filter-item" />
  35. </div>
  36. <el-input
  37. v-else
  38. v-model="params.quantity"
  39. style="width: 80px;"
  40. class="filter-item"
  41. />
  42. <el-autocomplete
  43. class="inline-input"
  44. v-model="params.unit"
  45. :fetch-suggestions="queryNutrientUnits"
  46. placeholder="单位关键词"
  47. />
  48. NRV%:
  49. <el-input
  50. v-model="params.nrvPercent"
  51. style="width: 80px;"
  52. class="filter-item"
  53. />
  54. Nv_Spec计量:
  55. <el-input
  56. v-model="params.nvSpec"
  57. style="width: 80px;"
  58. class="filter-item"
  59. />
  60. <el-autocomplete
  61. class="inline-input"
  62. v-model="params.nvSpecUnit"
  63. :fetch-suggestions="queryUnits"
  64. placeholder="单位关键词"
  65. />
  66. 信息来源:
  67. <el-autocomplete
  68. v-model="params.source"
  69. :fetch-suggestions="querySources"
  70. placeholder="请输入信息来源"
  71. />
  72. <el-button
  73. class="filter-item"
  74. style="margin-left: 10px;"
  75. type="primary"
  76. @click="addNutrient">
  77. 添加
  78. </el-button>
  79. </div>
  80. <el-table
  81. :key="tableKey"
  82. v-loading="listLoading"
  83. :data="list"
  84. border
  85. fit
  86. highlight-current-row
  87. style="width: 100%;margin-top: 10px"
  88. >
  89. <el-table-column type="index" label="序号" align="center" width="60px" />
  90. <el-table-column label="营养素名称" align="center">
  91. <template slot-scope="{row}">
  92. <span>{{ row.nutrientName }}</span>
  93. </template>
  94. </el-table-column>
  95. <el-table-column label="营养素计量" align="center" :width="150">
  96. <template slot-scope="{row}">
  97. <template v-if="row.edit">
  98. <el-radio v-model="row.radio" :label="0" @change="radioChange(row)">值</el-radio>
  99. <el-radio v-model="row.radio" :label="1" @change="radioChange(row)">范围</el-radio>
  100. <el-radio v-model="row.radio" :label="2" @change="radioChange(row)">误差</el-radio>
  101. <div v-if="row.radio === 1" style="display: inline-block">
  102. 大于等于:
  103. <el-input v-model="row.quantityMin" />
  104. 小于等于:
  105. <el-input v-model="row.quantityMax" />
  106. </div>
  107. <div v-else-if="row.radio === 2" style="display: inline-block">
  108. 基准:
  109. <el-input v-model="row.quantity" />
  110. ±:
  111. <el-input v-model="row.stdError" />
  112. </div>
  113. <el-input
  114. v-else
  115. v-model="row.quantity"
  116. style="width: 80px;"
  117. class="filter-item"
  118. />
  119. </template>
  120. <span v-else>{{ row | nutrientQuantityFilter }}</span>
  121. </template>
  122. </el-table-column>
  123. <el-table-column label="计量单位" align="center" width="150">
  124. <template slot-scope="{row}">
  125. <template v-if="row.edit">
  126. <el-autocomplete v-model="row.unit" :fetch-suggestions="queryUnits" placeholder="单位关键词" />
  127. </template>
  128. <span v-else>{{ row.unit }}</span>
  129. </template>
  130. </el-table-column>
  131. <el-table-column label="NRV%" align="center" width="80">
  132. <template slot-scope="{row}">
  133. <template v-if="row.edit">
  134. <el-input v-model="row.nrvPercent"/>
  135. </template>
  136. <span v-else>{{ row.nrvPercent }}</span>
  137. </template>
  138. </el-table-column>
  139. <el-table-column label="Nv_Spec" align="center" width="80">
  140. <template slot-scope="{row}">
  141. <template v-if="row.edit">
  142. <el-input v-model="row.nvSpec" class="filter-item" />
  143. </template>
  144. <span v-else>{{ row.nvSpec }}</span>
  145. </template>
  146. </el-table-column>
  147. <el-table-column label="计量单位" align="center" width="150">
  148. <template slot-scope="{row}">
  149. <template v-if="row.edit">
  150. <el-autocomplete v-model="row.nvSpecUnit" :fetch-suggestions="queryUnits" placeholder="单位关键词" />
  151. </template>
  152. <span v-else>{{ row.nvSpecUnit }}</span>
  153. </template>
  154. </el-table-column>
  155. <el-table-column label="信息来源" align="center" width="200">
  156. <template slot-scope="{row}">
  157. <template v-if="row.edit">
  158. <el-autocomplete
  159. v-model="row.source"
  160. :fetch-suggestions="querySources"
  161. placeholder="请输入信息来源"
  162. />
  163. </template>
  164. <span v-else>{{ row.source }}</span>
  165. </template>
  166. </el-table-column>
  167. <el-table-column label="信息来源备注" align="center" width="200">
  168. <template slot-scope="{row}">
  169. <template v-if="row.edit">
  170. <el-input type="text" :rows="2" v-model="row.sourceNote" />
  171. </template>
  172. <span v-else>{{ row.sourceNote }}</span>
  173. </template>
  174. </el-table-column>
  175. <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="320" fixed="right">
  176. <template slot-scope="{row}">
  177. <template v-if="row.edit">
  178. <el-button
  179. type="success"
  180. size="mini"
  181. @click="confirmEdit(row)"
  182. >
  183. 提交
  184. </el-button>
  185. <el-button
  186. type="danger"
  187. size="mini"
  188. @click="fetchData"
  189. >
  190. 取消
  191. </el-button>
  192. </template>
  193. <template v-else>
  194. <el-button size="mini" type="primary" @click="handleEdit(row)">编辑</el-button>
  195. <el-button size="mini" type="primary" @click="updateSort(row, 0)">上移</el-button>
  196. <el-button size="mini" type="primary" @click="updateSort(row, 1)">下移</el-button>
  197. <el-button size="mini" type="danger" @click="confirmRemoveNutrient(row)">
  198. 删除
  199. </el-button>
  200. </template>
  201. </template>
  202. </el-table-column>
  203. </el-table>
  204. </div>
  205. </template>
  206. <script>
  207. import { getNutrientList, addFoodNutrient, updateFoodNutrient, removeFoodNutrient,
  208. updateFoodNutrientSort, confirmDeleteFoodNutrient } from '@/api/food'
  209. import { getList, getNutrientUnits } from '@/api/nutrient'
  210. import { getList as getUnits } from '@/api/unit'
  211. export default {
  212. name: 'AddNutrient',
  213. created() {
  214. this.foodId = this.$route.params && this.$route.params.id
  215. this.fetchData()
  216. this.queryNutrients()
  217. },
  218. filters: {
  219. nutrientQuantityFilter(row) {
  220. if (row.stdError) {
  221. return `${row.quantity} ± ${row.stdError}`
  222. }else if (row.quantityMin && row.quantityMax) {
  223. return `${row.quantityMin} ~ ${row.quantityMax}`
  224. } else if (row.quantityMin) {
  225. return `≥${row.quantityMin}`
  226. } else if (row.quantityMax) {
  227. return `≤${row.quantityMax}`
  228. } else {
  229. return row.quantity
  230. }
  231. }
  232. },
  233. mounted() {
  234. this.$nextTick(() => {
  235. this.$refs.nutrientSelect.focus()
  236. })
  237. },
  238. data() {
  239. return {
  240. tableKey: 0,
  241. listLoading: false,
  242. units: [],
  243. foodId: '',
  244. list: [],
  245. nutrients: [],
  246. params: { source: '营养标签', radio: 0, nvSpec: 100, nvSpecUnit: '克' },
  247. loading: false,
  248. unitLoading: false,
  249. sources: [{ value: "营养标签" }, { value: "食品官方资料" }, { value: "计算值" }]
  250. }
  251. },
  252. methods: {
  253. fetchData() {
  254. this.listLoading = true
  255. getNutrientList(this.foodId).then(res => {
  256. this.list = res.data
  257. this.listLoading = false
  258. if (this.list.length > 0) {
  259. this.$set(this.params, "nvSpec", this.list[0].nvSpec)
  260. this.$set(this.params, "nvSpecUnit", this.list[0].nvSpecUnit)
  261. } else {
  262. this.$set(this.params, 'nvSpec', 100)
  263. this.$set(this.params, 'nvSpecUnit', '克')
  264. }
  265. })
  266. },
  267. addNutrient() {
  268. this.params.foodId = this.foodId
  269. addFoodNutrient(this.foodId, this.params).then(res => {
  270. this.params = { source: '营养标签', radio: 0 }
  271. this.fetchData()
  272. this.$notify.success('添加营养素成功')
  273. this.$refs.nutrientSelect.focus()
  274. }).catch(res => {
  275. this.$message.error(res.data.message)
  276. })
  277. },
  278. confirmEdit(row) {
  279. updateFoodNutrient(this.foodId, row.nutrientId, row).then(res => {
  280. this.fetchData()
  281. this.$notify.success('提交成功')
  282. }).catch(res => {
  283. this.$message.error(res.data.message)
  284. })
  285. },
  286. confirmRemoveNutrient(row) {
  287. confirmDeleteFoodNutrient(row.foodId, row.nutrientId).then(res => {
  288. if (!res.data) {
  289. this.$confirm('由于单位转换的原因,删除此条营养素关联数据,将会导致该食物被删除,是否继续?', '提示', {
  290. confirmButtonText: '确定',
  291. cancelButtonText: '取消',
  292. type: 'warning'
  293. }).then(() => {
  294. this.removeNutrient(row)
  295. })
  296. } else {
  297. this.removeNutrient(row)
  298. }
  299. }).catch(res => {
  300. this.$message.error(res.data.message)
  301. })
  302. },
  303. removeNutrient(row) {
  304. removeFoodNutrient(row.foodId, row.nutrientId).then(res => {
  305. this.fetchData()
  306. this.$notify.success('删除营养素成功')
  307. this.$refs.nutrientSelect.focus()
  308. }).catch(res => {
  309. this.$message.error(res.data.message)
  310. })
  311. },
  312. queryNutrients(query) {
  313. getList({ query }).then(res => {
  314. this.nutrients = res.data.list
  315. }).catch(() => {
  316. this.nutrients = []
  317. })
  318. },
  319. queryNutrientUnits(query, cb) {
  320. let units = []
  321. getNutrientUnits(this.params.nutrientId).then(res => {
  322. res.data.forEach(item => units.push({ value: item }))
  323. cb(units)
  324. })
  325. },
  326. queryUnits(query, cb) {
  327. let units = []
  328. getUnits({ query }).then(res => {
  329. res.data.list.forEach(item => units.push({ value: item.name }))
  330. cb(units)
  331. })
  332. },
  333. updateSort(row, type) {
  334. updateFoodNutrientSort(this.foodId, row.nutrientId, { type }).then(res => {
  335. this.$notify.success('提交成功')
  336. this.fetchData()
  337. }).catch(res => {
  338. this.$message.error(res.data.message)
  339. })
  340. },
  341. querySources(query, cb) {
  342. let sources = this.sources
  343. let results = query ? sources.filter(this.sourcesFilter(query)) : sources;
  344. cb(results)
  345. },
  346. sourcesFilter(query) {
  347. return (restaurant) => {
  348. return (restaurant.value.toLowerCase().indexOf(query.toLowerCase()) === 0);
  349. };
  350. },
  351. handleEdit(row) {
  352. this.$set(row, 'edit', true)
  353. if (row.quantityMax || row.quantityMin) {
  354. this.$set(row, 'radio', 1)
  355. } else if (row.stdError) {
  356. this.$set(row, 'radio', 2)
  357. } else {
  358. this.$set(row, 'radio', 0)
  359. }
  360. },
  361. radioChange(row) {
  362. this.$set(row, 'stdError', '')
  363. this.$set(row, 'quantityMin', '')
  364. this.$set(row, 'quantityMax', '')
  365. this.$set(row, 'quantity', '')
  366. },
  367. nutrientChanged(value) {
  368. for (let nutrient of this.nutrients) {
  369. if (nutrient.id === value) {
  370. this.$set(this.params, 'unit', nutrient.baseUnit)
  371. break
  372. }
  373. }
  374. }
  375. }
  376. }
  377. </script>
  378. <style scoped>
  379. </style>