table.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <div>
  3. <a-spin :spinning="spinning" tip="Loading...">
  4. <a-card size="small" class="card-table">
  5. <!-- 搜索条件 -->
  6. <div class="table-page-search-wrapper newTableSearchName" ref="tableSearch">
  7. <a-form-model
  8. id="dayReportList-form"
  9. ref="ruleForm"
  10. class="form-model-con"
  11. layout="inline"
  12. :rules="rules"
  13. :model="queryParam"
  14. @keyup.enter.native="handleSearch">
  15. <a-row :gutter="15">
  16. <a-col :md="6" :sm="24">
  17. <a-form-model-item label="月份" prop="month">
  18. <a-month-picker
  19. placeholder="请选择月份"
  20. locale="zh-cn"
  21. id="dayReportList-month"
  22. v-model="queryParam.month"
  23. :allowClear="false"
  24. @change="onChange"
  25. :disabled-date="disabledDate"
  26. style="width: 100%;"/>
  27. </a-form-model-item>
  28. </a-col>
  29. <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
  30. <a-button type="primary" @click="handleSearch" :disabled="disabled" id="dayReportList-refresh">查询</a-button>
  31. <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="dayReportList-reset">重置</a-button>
  32. <a-button
  33. style="margin-left: 5px"
  34. type="primary"
  35. class="button-warning"
  36. @click="handleExport"
  37. :disabled="tableData.length==0"
  38. v-if="$hasPermissions('B_National_Sales_Daily_Export')&&activeKey==0
  39. ||$hasPermissions('B_Project_Sales_Daily_Export')&&activeKey==1
  40. ||$hasPermissions('B_Project_Sales_Daily_Export_2')&&activeKey==2
  41. ||$hasPermissions('M_Regional_Sales_Daily_Export')&&activeKey==3"
  42. :loading="exportLoading"
  43. id="dayReportList-export">导出</a-button>
  44. </a-col>
  45. </a-row>
  46. </a-form-model>
  47. </div>
  48. <!-- 列表 -->
  49. <table0
  50. v-if="activeKey == 0"
  51. ref="table0"
  52. :month="queryParam.month"
  53. :columns="columns"
  54. :tableData="tableData"
  55. :nowDate="nowDate"
  56. ></table0>
  57. <table1
  58. v-if="activeKey == 1"
  59. ref="table1"
  60. :month="queryParam.month"
  61. :columns="columns"
  62. :tableData="tableData"
  63. :nowDate="nowDate"
  64. ></table1>
  65. <table2
  66. v-if="activeKey == 2"
  67. ref="table2"
  68. :month="queryParam.month"
  69. :columns="columns"
  70. :tableData="tableData"
  71. :nowDate="nowDate"
  72. ></table2>
  73. <table3
  74. v-if="activeKey == 3"
  75. ref="table3"
  76. :month="queryParam.month"
  77. :columns="columns"
  78. :tableData="tableData"
  79. :nowDate="nowDate"
  80. ></table3>
  81. </a-card>
  82. </a-spin>
  83. </div>
  84. </template>
  85. <script>
  86. import { commonMixin } from '@/utils/mixin'
  87. import XLSX from 'xlsx-js-style'
  88. // 组件
  89. import moment from 'moment'
  90. import table0 from './table0'
  91. import table1 from './table1'
  92. import table2 from './table2'
  93. import table3 from './table3'
  94. // 接口
  95. import { salesReportDailyList } from '@/api/reportData.js'
  96. export default {
  97. name: 'RegionTypeSalesReportList',
  98. mixins: [commonMixin],
  99. components: { table0, table1, table2, table3 },
  100. data () {
  101. return {
  102. spinning: false,
  103. activeKey: 0, // 切换显示页面值 0 各分区品类实售明细 1各区域品类实售明细
  104. advanced: true, // 高级搜索 展开/关闭
  105. disabled: false, // 查询、重置按钮是否可操作
  106. exportLoading: false, // 导出按钮加载状态
  107. tableHeight: 0, // 表格高度
  108. tabsList: [{ id: 0, name: '全国销售日报' }, { id: 1, name: '项目销售日报1' }, { id: 2, name: '项目销售日报2' }, { id: 3, name: '分区销售日报' }], // tab切换data
  109. columns: [], // 表头数据
  110. tableData: [], // 表格数据
  111. totalNums: 0, // 总条数
  112. // 查询参数
  113. queryParam: {
  114. month: moment().format('YYYY-MM') // 月份默认值
  115. },
  116. rules: {
  117. 'month': [{ required: true, message: '请选择月份', trigger: 'change' }]
  118. },
  119. nowDate: ''
  120. }
  121. },
  122. methods: {
  123. // 月份 change
  124. onChange (date, dateString) {
  125. this.queryParam.month = dateString
  126. },
  127. // 月份选择范围
  128. disabledDate (current) {
  129. return current && current > moment().endOf('day')
  130. },
  131. // tabs切换 change时间
  132. callback (key) {
  133. this.activeKey = key
  134. const _this = this
  135. _this.$nextTick(() => {
  136. this.resetSearchForm()
  137. })
  138. },
  139. // 查询
  140. handleSearch () {
  141. const _this = this
  142. this.$refs.ruleForm.validate(valid => {
  143. if (valid) {
  144. _this.getTableData()
  145. } else {
  146. _this.$message.error('请选择月份')
  147. return false
  148. }
  149. })
  150. },
  151. // 重置
  152. resetSearchForm () {
  153. this.$refs.ruleForm.resetFields()
  154. this.tableData = []
  155. this.totalNums = 0
  156. this.totalNums = []
  157. },
  158. exportTable (dom, title) {
  159. const zim = 'A、B、C、D、E、F、G、H、I、J、K、L、M、N、O、P、Q、R、S、T、U、V、W、X、Y、Z‌'.split('、')
  160. var ws = XLSX.utils.table_to_sheet(dom)
  161. var wb = XLSX.utils.book_new()
  162. // console.log(ws)
  163. // 设置样式
  164. Object.keys(ws).forEach(cell => {
  165. if (cell[0] !== '!') {
  166. ws[cell].s = {
  167. alignment: {
  168. horizontal: 'center',
  169. vertical: 'center'
  170. }
  171. }
  172. }
  173. })
  174. // 表格0
  175. if (this.activeKey == 0) {
  176. const ri = [6, 11, 20, 23]
  177. this.columns.forEach((item, i) => {
  178. if (i > 1) {
  179. const cols = zim[i]
  180. ri.forEach((row, j) => {
  181. const a = ws[cols + row]
  182. if (a) {
  183. a.z = (a.v * 100) + '%' // 设置单元格格式为百分比
  184. }
  185. })
  186. }
  187. })
  188. }
  189. // 表格1表格2
  190. if (this.activeKey == 1 || this.activeKey == 2) {
  191. const cols = zim[this.columns.length - 1]
  192. Array(this.totalNums).fill().forEach((item, i) => {
  193. const a = ws[cols + (i + 3)]
  194. if (a) {
  195. a.z = (a.v * 100) + '%' // 设置单元格格式为百分比
  196. }
  197. })
  198. }
  199. // 表格3
  200. if (this.activeKey == 3) {
  201. const ci = [6, 8]
  202. ci.forEach((row) => {
  203. const cols = zim[row]
  204. Array(this.totalNums).fill().forEach((item, i) => {
  205. const a = ws[cols + (i + 4)]
  206. if (a) {
  207. a.z = (a.v * 100) + '%' // 设置单元格格式为百分比
  208. }
  209. })
  210. })
  211. }
  212. // 4. 设置列宽
  213. ws['!cols'] = Array(this.columns.length).fill().map(() => ({ wch: this.activeKey == 3 ? 15 : 35 }))
  214. const timeStr = moment().format('YYYYMMDDHHmmss')
  215. // 5. 写入文件
  216. XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
  217. XLSX.writeFile(wb, this.queryParam.month.split('-')[1] + '月' + title + '-' + timeStr + '.xlsx')
  218. },
  219. // 导出
  220. handleExport () {
  221. const excelTitle = this.tabsList.find(item => item.id == this.activeKey)
  222. if (this.$refs.table0 && this.activeKey == 0) {
  223. this.totalNums = this.$refs.table0.list.length
  224. this.exportTable(this.$refs.table0.getTable(), excelTitle.name)
  225. } else if (this.$refs.table1 && this.activeKey == 1) {
  226. this.totalNums = this.$refs.table1.list.length
  227. this.exportTable(this.$refs.table1.getTable(), excelTitle.name)
  228. } else if (this.$refs.table2 && this.activeKey == 2) {
  229. this.totalNums = this.$refs.table2.list.length
  230. this.exportTable(this.$refs.table2.getTable(), excelTitle.name)
  231. } else if (this.$refs.table3 && this.activeKey == 3) {
  232. this.totalNums = this.$refs.table3.list.length
  233. this.exportTable(this.$refs.table3.getTable(), excelTitle.name)
  234. }
  235. },
  236. // 获取表格数据
  237. async getTableData () {
  238. const reportType = ['COUNTRY_SALES_DAY', 'DailyPL', 'DailyPLJL', 'AREA_SALES_DAY'][this.activeKey]
  239. const reportDate = this.queryParam.month + '-01 00:00:00'
  240. this.nowDate = moment().format('DD日HH点mm分')
  241. this.spinning = true
  242. const data = await salesReportDailyList({ reportType, reportDate }).then(res => res.data)
  243. if (data) {
  244. this.tableData = data
  245. // 全国销售日报
  246. if (this.activeKey == 0) {
  247. this.columns = ['', '']
  248. data.forEach(item => {
  249. this.columns.push(item.subareaName)
  250. })
  251. }
  252. // 项目销售日报1
  253. if (this.activeKey == 1) {
  254. this.columns = this.getColumns()
  255. }
  256. // 项目销售日报2
  257. if (this.activeKey == 2) {
  258. this.columns = [1, 2, 3, 4, 5]
  259. }
  260. // 分区销售报表
  261. if (this.activeKey == 3 && this.$refs.table3) {
  262. this.columns = this.$refs.table3.columns
  263. }
  264. // 初始化页面
  265. this.$nextTick(() => {
  266. if (this.$refs['table' + this.activeKey]) {
  267. this.$refs['table' + this.activeKey].pageInit()
  268. }
  269. })
  270. this.spinning = false
  271. }
  272. },
  273. getColumns () {
  274. const row = this.tableData[0]
  275. const ret = []
  276. if (row) {
  277. row.detailList[0].forEach(item => {
  278. ret.push(item)
  279. })
  280. ret.push(row.bymb)
  281. ret.push(row.wcl)
  282. }
  283. return ret
  284. },
  285. // 初始化
  286. pageInit (activeKey) {
  287. this.activeKey = activeKey
  288. }
  289. },
  290. mounted () {
  291. if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
  292. this.pageInit()
  293. }
  294. }
  295. }
  296. </script>
  297. <style lang="less" scoped>
  298. .card-table{
  299. height: calc(100vh - 140px);
  300. overflow: auto;
  301. }
  302. </style>