table.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. // 导出表格excel,dom 表格对象,title 导出表格名称
  159. exportTable (dom, title) {
  160. 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('、')
  161. var ws = XLSX.utils.table_to_sheet(dom)
  162. // console.log(ws)
  163. // 设置样式
  164. Object.keys(ws).forEach(cell => {
  165. if (cell[0] !== '!') {
  166. // 设置表头样式
  167. if (cell === 'A1') {
  168. ws[cell].s = {
  169. font: { bold: true, color: { rgb: 'FFFFFF' } },
  170. fill: { fgColor: { rgb: '4472C4' } },
  171. alignment: { horizontal: 'center' }
  172. }
  173. } else {
  174. ws[cell].s = {
  175. alignment: {
  176. horizontal: 'center',
  177. vertical: 'center'
  178. }
  179. }
  180. }
  181. }
  182. })
  183. // 设置百分比格式
  184. // 表格0
  185. if (this.activeKey == 0) {
  186. const ri = [6, 11, 20, 23]
  187. this.columns.forEach((item, i) => {
  188. if (i > 1) {
  189. const cols = zim[i]
  190. ri.forEach((row, j) => {
  191. const a = ws[cols + row]
  192. if (a) {
  193. a.v = Number(a.v * 100).toFixed(2) + '%' // 设置单元格格式为百分比
  194. a.t = 's'
  195. }
  196. })
  197. }
  198. })
  199. }
  200. // 表格1表格2
  201. if (this.activeKey == 1 || this.activeKey == 2) {
  202. const cols = zim[this.columns.length - 1]
  203. Array(this.totalNums).fill().forEach((item, i) => {
  204. const a = ws[cols + (i + 3)]
  205. console.log(a)
  206. if (a) {
  207. a.v = Number(a.v * 100).toFixed(2) + '%' // 设置单元格格式为百分比
  208. a.t = 's'
  209. }
  210. })
  211. }
  212. // 表格3
  213. if (this.activeKey == 3) {
  214. const ci = [6, 8]
  215. ci.forEach((row) => {
  216. const cols = zim[row]
  217. Array(this.totalNums).fill().forEach((item, i) => {
  218. const a = ws[cols + (i + 4)]
  219. if (a) {
  220. a.v = Number(a.v * 100).toFixed(2) + '%' // 设置单元格格式为百分比
  221. a.t = 's'
  222. }
  223. })
  224. })
  225. }
  226. // 4. 设置列宽
  227. ws['!cols'] = Array(this.columns.length).fill().map(() => ({ wch: this.activeKey == 3 ? 15 : 35 }))
  228. const timeStr = moment().format('YYYYMMDDHHmmss')
  229. // 创建工作簿并导出
  230. var wb = XLSX.utils.book_new()
  231. // 5. 写入文件
  232. XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
  233. XLSX.writeFile(wb, this.queryParam.month.split('-')[1] + '月' + title + '-' + timeStr + '.xlsx')
  234. },
  235. // 点击导出
  236. handleExport () {
  237. const excelTitle = this.tabsList.find(item => item.id == this.activeKey)
  238. if (this.$refs.table0 && this.activeKey == 0) {
  239. this.totalNums = this.$refs.table0.list.length
  240. this.exportTable(this.$refs.table0.getTable(), excelTitle.name)
  241. } else if (this.$refs.table1 && this.activeKey == 1) {
  242. this.totalNums = this.$refs.table1.list.length
  243. this.exportTable(this.$refs.table1.getTable(), excelTitle.name)
  244. } else if (this.$refs.table2 && this.activeKey == 2) {
  245. this.totalNums = this.$refs.table2.list.length
  246. this.exportTable(this.$refs.table2.getTable(), excelTitle.name)
  247. } else if (this.$refs.table3 && this.activeKey == 3) {
  248. this.totalNums = this.$refs.table3.list.length
  249. this.exportTable(this.$refs.table3.getTable(), excelTitle.name)
  250. }
  251. },
  252. // 获取表格数据
  253. async getTableData () {
  254. const reportType = ['COUNTRY_SALES_DAY', 'DailyPL', 'DailyPLJL', 'AREA_SALES_DAY'][this.activeKey]
  255. const reportDate = this.queryParam.month + '-01 00:00:00'
  256. this.nowDate = moment().format('DD日HH点mm分')
  257. this.spinning = true
  258. const data = await salesReportDailyList({ reportType, reportDate }).then(res => res.data)
  259. if (data && data.length) {
  260. this.tableData = data
  261. // 全国销售日报
  262. if (this.activeKey == 0) {
  263. this.columns = ['', '']
  264. data.forEach(item => {
  265. this.columns.push(item.subareaName)
  266. })
  267. }
  268. // 项目销售日报1
  269. if (this.activeKey == 1) {
  270. this.columns = this.getColumns()
  271. }
  272. // 项目销售日报2
  273. if (this.activeKey == 2) {
  274. this.columns = [1, 2, 3, 4, 5]
  275. }
  276. // 分区销售报表
  277. if (this.activeKey == 3 && this.$refs.table3) {
  278. this.columns = this.$refs.table3.columns
  279. }
  280. // 初始化页面
  281. this.$nextTick(() => {
  282. if (this.$refs['table' + this.activeKey]) {
  283. this.$refs['table' + this.activeKey].pageInit()
  284. }
  285. })
  286. } else {
  287. this.tableData = []
  288. }
  289. this.spinning = false
  290. },
  291. // 项目销售日报1列数据
  292. getColumns () {
  293. const row = this.tableData[0]
  294. const ret = []
  295. if (row) {
  296. row.detailList[0].forEach(item => {
  297. ret.push(item)
  298. })
  299. ret.push(row.bymb)
  300. ret.push(row.wcl)
  301. }
  302. return ret
  303. },
  304. // 初始化
  305. pageInit (activeKey) {
  306. this.activeKey = activeKey
  307. }
  308. },
  309. mounted () {
  310. if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
  311. this.pageInit()
  312. }
  313. }
  314. }
  315. </script>
  316. <style lang="less" scoped>
  317. .card-table{
  318. height: calc(100vh - 140px);
  319. overflow: auto;
  320. }
  321. </style>