ermcpReport.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/2/4 11:27
  4. * @Modify : 2021/2/4 11:27
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "mtp2_if/db"
  10. "mtp2_if/mtpcache"
  11. "mtp2_if/utils"
  12. )
  13. // ErmcpReportDayFR 财务日报表
  14. type ErmcpReportDayFR struct {
  15. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'"` // 日照时期(yyyyMMdd)
  16. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  17. COLLECTMONEYCOUNT int32 `json:"collectmoneycount" xorm:"'COLLECTMONEYCOUNT'"` // 今日收款笔数
  18. COLLECTMONEYAMOUNT float64 `json:"collectmoneyamount" xorm:"'COLLECTMONEYAMOUNT'"` // 今日收款金额
  19. PAYMONEYCOUNT int32 `json:"paymoneycount" xorm:"'PAYMONEYCOUNT'"` // 今日付款笔数
  20. PAYMONEYAMOUNT float64 `json:"paymoneyamount" xorm:"'PAYMONEYAMOUNT'"` // 今日付款金额
  21. COLLECTINVOICECOUNT int32 `json:"collectinvoicecount" xorm:"'COLLECTINVOICECOUNT'"` // 今日开票笔数
  22. COLLECTINVOICEAMOUNT float64 `json:"collectinvoiceamount" xorm:"'COLLECTINVOICEAMOUNT'"` // 今日开票金额
  23. PAYINVOICECOUNT int32 `json:"payinvoicecount" xorm:"'PAYINVOICECOUNT'"` // 今日收票笔数
  24. PAYINVOICEAMOUNT float64 `json:"payinvoiceamount" xorm:"'PAYINVOICEAMOUNT'"` // 今日收票金额
  25. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  26. BeginDate string `json:"-"` // 开始日期
  27. EndDate string `json:"-"` // 结束日期
  28. }
  29. // Calc 数据处理
  30. func (r *ErmcpReportDayFR) Calc() {
  31. }
  32. func (r *ErmcpReportDayFR) buildSql() string {
  33. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  34. " t.AREAUSERID," +
  35. " t.COLLECTMONEYCOUNT," +
  36. " t.COLLECTMONEYAMOUNT," +
  37. " t.PAYMONEYCOUNT," +
  38. " t.PAYMONEYAMOUNT," +
  39. " t.COLLECTINVOICECOUNT," +
  40. " t.COLLECTINVOICEAMOUNT," +
  41. " t.PAYINVOICECOUNT," +
  42. " t.PAYINVOICEAMOUNT," +
  43. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh:mi:ss') UPDATETIME" +
  44. " FROM RECKON_ERMCP_AREAFR t" +
  45. " WHERE 1 = 1"
  46. // 查询条件
  47. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  48. if r.RECKONDATE != "" {
  49. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  50. } else if r.BeginDate != "" && r.BeginDate == r.EndDate {
  51. sqlId.And("t.RECKONDATE", r.BeginDate)
  52. } else {
  53. if r.BeginDate != "" {
  54. sqlId.BiggerOrEq("t.RECKONDATE", r.BeginDate)
  55. }
  56. if r.EndDate != "" {
  57. sqlId.LessOrEq("t.RECKONDATE", r.EndDate)
  58. }
  59. }
  60. return sqlId.String()
  61. }
  62. // GetDataEx 获取日报表
  63. func (r *ErmcpReportDayFR) GetDataEx() (interface{}, error) {
  64. sData := make([]ErmcpReportDayFR, 0)
  65. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  66. return sData, err
  67. }
  68. // ErmcpReportDayFRKx 财务日报表/款项
  69. type ErmcpReportDayFRKx struct {
  70. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  71. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  72. }
  73. // GetDataEx 获取日报表款项记录
  74. func (r *ErmcpReportDayFRKx) GetDataEx() (interface{}, error) {
  75. m := ErmcpReportOPLog{USERID: r.USERID, LogTypeFilter: "8,9,10", TRADEDATE: r.TRADEDATE}
  76. return m.GetDataEx()
  77. }
  78. // ErmcpReportOPLog 报表合同操作记录通用查询
  79. type ErmcpReportOPLog struct {
  80. LOGID string `json:"logid" xorm:"'lOGID'"` // 流水ID(604+Unix秒时间戳(10位)+xxxxxx)
  81. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 - 1:套保计划 2:现货合同
  82. OPERATELOGTYPE int32 `json:"operatelogtype" xorm:"'OPERATELOGTYPE'"` // 操作流水类型 -
  83. RELATEDID string `json:"relatedid" xorm:"'RELATEDID'"` // 现货合同ID\套保计划
  84. LOGVALUE string `json:"logvalue" xorm:"'LOGVALUE'"` // 数值
  85. LOGDATETIME string `json:"logdatetime" xorm:"'LOGDATETIME'"` // 流水日期(时间)
  86. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  87. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 操作人
  88. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  89. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  90. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  91. RELATEDNO string `json:"relatedno" xorm:"'RELATEDNO'"` // 合同编号
  92. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  93. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  94. UNITID int32 `json:"-" xorm:"'UNITID'"` // 现货商品单位id
  95. ENUMDICNAME string `json:"enumdicName"` // 单位名称
  96. OPTYPENAME string `json:"optypename"` // 流水类型名称
  97. LOGTYPENAME string `json:"logtypename"` // 合同类型(名称)
  98. APPLYNAME string `json:"applyname"` // 操作人名称
  99. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  100. MODELNAME string `json:"modelname" xorm:"'modelname'"` // 型号名称
  101. LogTypeFilter string `json:"-"` // 查询日志类型, 逗号隔开(如 1,2,4)
  102. }
  103. // Calc 处理数据
  104. func (r *ErmcpReportOPLog) Calc() {
  105. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  106. r.APPLYNAME = mtpcache.GetUserNameByUserId(r.APPLYID)
  107. if r.APPLYNAME == "" {
  108. // APPLYID 可能存的是loginId
  109. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  110. }
  111. //1:点价价格 2:点价数量 3:结算量 4:其它费用 5:追加保证金 6溢短金额 7:调整金额 8:付款金额 9:收款金额 10:退款金额
  112. //11:收票金额 12:开票金额 13:提交审核(合同) 14:审核通过(合同) 15:审核拒绝(合同) 16:合同撤回 17:提交审核(计划)
  113. //18:审核通过(计划) 19:审核拒绝(计划) 20:计划撤回 21:正常完结(合同) 22:异常终止(合同) 23:退还保证金
  114. // 数据库注释与返回值映身关系: 结算量->确定量, 收款->收款金额, 退款->退款金额, 付款->付款金额, 收票->收票金额, 开票->开票金额
  115. sDes := []string{"点价价格", "点价数量", "确定量", "其它费用", "追加保证金", "溢短金额", "调整金额", "付款", "收款", "退款",
  116. "收票", "开票", "提交审核(合同)", "审核通过(合同)", "审核拒绝(合同)", "合同撤回", "提交审核(计划)",
  117. "审核通过(计划)", "审核拒绝(计划)", "计划撤回", "正常完结(合同)", "异常终止(合同)", "退还保证金"}
  118. if r.OPERATELOGTYPE >= 1 && r.OPERATELOGTYPE <= 23 {
  119. r.OPTYPENAME = sDes[r.OPERATELOGTYPE-1]
  120. }
  121. // 收款款项:采购合同的退款、销售合同的收款应用正数显示,付款款项:采购合同的付款、销售合同的退款应用负数显示
  122. if r.CONTRACTTYPE == 1 {
  123. r.LOGTYPENAME = "采购"
  124. // 采购合同/退款 正数
  125. if r.OPERATELOGTYPE == 10 && len(r.LOGVALUE) > 0 {
  126. if r.LOGVALUE[0] == '-' {
  127. r.LOGVALUE = r.LOGVALUE[1:]
  128. }
  129. }
  130. // 采购合同/付款 负数
  131. if r.OPERATELOGTYPE == 8 && len(r.LOGVALUE) > 0 {
  132. if r.LOGVALUE[0] != '-' {
  133. r.LOGVALUE = "-" + r.LOGVALUE
  134. }
  135. }
  136. } else if r.CONTRACTTYPE == -1 {
  137. r.LOGTYPENAME = "销售"
  138. // 销售合同/收款 正数
  139. if r.OPERATELOGTYPE == 9 && len(r.LOGVALUE) > 0 {
  140. if r.LOGVALUE[0] == '-' {
  141. r.LOGVALUE = r.LOGVALUE[1:]
  142. }
  143. }
  144. // 销售合同/退款 负数
  145. if r.OPERATELOGTYPE == 10 && len(r.LOGVALUE) > 0 {
  146. if r.LOGVALUE[0] != '-' {
  147. r.LOGVALUE = "-" + r.LOGVALUE
  148. }
  149. }
  150. }
  151. // 去除多余的0,最后4个0
  152. if r.LOGVALUE[len(r.LOGVALUE)-4:] == "0000" {
  153. r.LOGVALUE = r.LOGVALUE[:len(r.LOGVALUE)-4]
  154. }
  155. if r.BIZTYPE == 1 {
  156. r.LOGTYPENAME += "计划"
  157. } else if r.BIZTYPE == 2 {
  158. r.LOGTYPENAME += "合同"
  159. }
  160. }
  161. func (r *ErmcpReportOPLog) buildSql() string {
  162. var sqlId utils.SQLVal = "SELECT to_char(t.LOGID) LOGID," +
  163. " t.BIZTYPE," +
  164. " t.OPERATELOGTYPE," +
  165. " to_char(t.RELATEDID) RELATEDID," +
  166. " t.LOGVALUE," +
  167. " to_char(t.LOGDATETIME, 'yyyy-mm-dd hh24:mi:ss') LOGDATETIME," +
  168. " t.TRADEDATE," +
  169. " t.APPLYID," +
  170. " t.CONTRACTTYPE," +
  171. " t.USERID," +
  172. " t.WRSTANDARDID," +
  173. " s.contractno relatedno," +
  174. " s.spotgoodsbrandid," +
  175. " s.spotgoodsmodelid," +
  176. " w.wrstandardname," +
  177. " w.wrstandardcode," +
  178. " w.unitid," +
  179. " gb.brandname," +
  180. " gm.modelname" +
  181. " FROM ERMCP_CONTRACTOPERATELOG t" +
  182. " inner join ermcp_spotcontract s" +
  183. " on t.RELATEDID = s.spotcontractid" +
  184. " and t.userid = %v" +
  185. " left join wrstandard w" +
  186. " on t.wrstandardid = w.wrstandardid" +
  187. " left join spotgoodsbrand gb" +
  188. " on s.spotgoodsbrandid = gb.brandid" +
  189. " left join spotgoodsmodel gm" +
  190. " on s.spotgoodsmodelid = gm.modelid" +
  191. " WHERE t.biztype = 2"
  192. sqlId = utils.SQLVal(fmt.Sprintf(sqlId.String(), r.USERID))
  193. // 筛选条件
  194. sqlId.And("t.TRADEDATE", r.TRADEDATE)
  195. if r.WRSTANDARDID > 0 {
  196. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  197. }
  198. if r.LogTypeFilter != "" {
  199. sqlId.Join(fmt.Sprintf(" and t.OPERATELOGTYPE in(%v)", r.LogTypeFilter))
  200. }
  201. return sqlId.String()
  202. }
  203. // GetDataEx 获取日志记录
  204. func (r *ErmcpReportOPLog) GetDataEx() (interface{}, error) {
  205. sData := make([]ErmcpReportOPLog, 0)
  206. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  207. for i := range sData {
  208. sData[i].Calc()
  209. }
  210. return sData, err
  211. }
  212. // ErmcpReportMonthFR 财务月报表
  213. type ErmcpReportMonthFR struct {
  214. CYCLETYPE int32 `json:"cycletype" xorm:"'cycletype'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  215. CYCLETIME string `json:"cycletime" xorm:"'cycletime'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  216. AREAUSERID int64 `json:"areauserid" xorm:"'areauserid'"` // 所属机构【原值】
  217. COLLECTMONEYCOUNT int32 `json:"collectmoneycount" xorm:"'collectmoneycount'"` // 今日收款笔数 【汇总】
  218. COLLECTMONEYAMOUNT float64 `json:"collectmoneyamount" xorm:"'collectmoneyamount'"` // 今日收款金额【汇总】
  219. PAYMONEYCOUNT int32 `json:"paymoneycount" xorm:"'paymoneycount'"` // 今日付款笔数【汇总】
  220. PAYMONEYAMOUNT float64 `json:"paymoneyamount" xorm:"'paymoneyamount'"` // 今日付款金额【汇总】
  221. COLLECTINVOICECOUNT int32 `json:"collectinvoicecount" xorm:"'collectinvoicecount'"` // 今日开票笔数【汇总】
  222. COLLECTINVOICEAMOUNT float64 `json:"collectinvoiceamount" xorm:"'collectinvoiceamount'"` // 今日开票金额【汇总】
  223. PAYINVOICECOUNT int32 `json:"payinvoicecount" xorm:"'payinvoicecount'"` // 今日收票笔数【汇总】
  224. PAYINVOICEAMOUNT float64 `json:"payinvoiceamount" xorm:"'payinvoiceamount'"` // 今日收票金额【汇总】
  225. UPDATETIME string `json:"updatetime" xorm:"'updatetime'"` // 更新时间
  226. DayFR []ErmcpReportDayFR `json:"dayFr"` // 日报表明细
  227. }
  228. // Calc 数据处理
  229. func (r *ErmcpReportMonthFR) Calc() {
  230. }
  231. func (r *ErmcpReportMonthFR) buildSql() string {
  232. var sqlId utils.SQLVal = "SELECT t.CYCLETYPE," +
  233. " t.CYCLETIME," +
  234. " t.AREAUSERID," +
  235. " t.COLLECTMONEYCOUNT," +
  236. " t.COLLECTMONEYAMOUNT," +
  237. " t.PAYMONEYCOUNT," +
  238. " t.PAYMONEYAMOUNT," +
  239. " t.COLLECTINVOICECOUNT," +
  240. " t.COLLECTINVOICEAMOUNT," +
  241. " t.PAYINVOICECOUNT," +
  242. " t.PAYINVOICEAMOUNT," +
  243. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh:mi:ss') UPDATETIME" +
  244. " FROM REPORT_ERMCP_AREAFR t" +
  245. " WHERE 1 = 1"
  246. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  247. sqlId.And("t.CYCLETYPE", 1)
  248. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  249. return sqlId.String()
  250. }
  251. // GetDataEx 获取月报表
  252. func (r *ErmcpReportMonthFR) GetDataEx() (interface{}, error) {
  253. sData := make([]ErmcpReportMonthFR, 0)
  254. // 月报表
  255. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  256. // 日报表明细
  257. if len(r.CYCLETIME) != 6 {
  258. // 月报表的日期应是6位,如 202101
  259. return nil, nil
  260. }
  261. t1 := r.CYCLETIME + "01"
  262. t2 := r.CYCLETIME + "31"
  263. for i := range sData {
  264. sData[i].Calc()
  265. dayM := ErmcpReportDayFR{AREAUSERID: r.AREAUSERID, BeginDate: t1, EndDate: t2}
  266. if dObj, err := dayM.GetDataEx(); err == nil {
  267. if d, ok := dObj.([]ErmcpReportDayFR); ok {
  268. sData[i].DayFR = d
  269. }
  270. }
  271. }
  272. return sData, err
  273. }
  274. // ErmcpReportDaySpot 现货日报表
  275. type ErmcpReportDaySpot struct {
  276. BUYPRICEDQTY float64 `json:"-" xorm:"'BUYPRICEDQTY'"` // 期末采购定价量
  277. SELLPRICEDQTY float64 `json:"-" xorm:"'SELLPRICEDQTY'"` // 期末销售定价量
  278. ORIBUYPRICEDQTY float64 `json:"-" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购定价量
  279. ORISELLPRICEDQTY float64 `json:"-" xorm:"'ORISELLPRICEDQTY'"` // 期初销售定价量
  280. TODAYBUYRECKONQTY float64 `json:"todaybuyreckonqty" xorm:"'TODAYBUYRECKONQTY'"` // 采购确定量
  281. TODAYSELLRECKONQTY float64 `json:"todaysellreckonqty" xorm:"'TODAYSELLRECKONQTY'"` // 销售确定量
  282. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'"` // 日照时期(yyyyMMdd)
  283. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  284. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构T
  285. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  286. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  287. UNITID int32 `json:"-" xorm:"'UNITID'"` // 现货商品单位id
  288. BUYINQTY float64 `json:"buyinqty" xorm:"'BUYINQTY'"` // 采购入库量
  289. SELLOUTQTY float64 `json:"selloutqty" xorm:"'SELLOUTQTY'"` // 销售出库量
  290. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  291. TOTALBUYPRICEDQTY float64 `json:"totalbuypricedqty"` // 采购定价量 = 期末 - 期初
  292. TOTALSELLPRICEDQTY float64 `json:"totalsellpricedqty"` // 销售定价量 = 期末 - 期初
  293. BeginDate string `json:"-"` // 开始日期
  294. EndDate string `json:"-"` // 结束日期
  295. }
  296. // Calc 数据处理
  297. func (r *ErmcpReportDaySpot) Calc() {
  298. r.TOTALBUYPRICEDQTY = r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  299. r.TOTALSELLPRICEDQTY = r.SELLPRICEDQTY - r.ORISELLPRICEDQTY
  300. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  301. }
  302. func (r *ErmcpReportDaySpot) buildSql() string {
  303. var sqlId utils.SQLVal = "with tmp as" +
  304. " (select t.userid," +
  305. " t.wrstandardid," +
  306. " t.reckondate," +
  307. " sum(t.todaybuyinqty) todaybuyinqty," +
  308. " sum(t.todayselloutqty) todayselloutqty" +
  309. " from Reckon_ERMCP_AreaStock t" +
  310. " group by t.userid, t.wrstandardid,t.reckondate)" +
  311. "select t.reckondate," +
  312. " t.BUYPRICEDQTY," +
  313. " t.SELLPRICEDQTY," +
  314. " t.Oribuypricedqty," +
  315. " t.Orisellpricedqty," +
  316. " t.TODAYBUYRECKONQTY," +
  317. " t.TODAYSELLRECKONQTY," +
  318. " t.RECKONDATE," +
  319. " t.WRSTANDARDID," +
  320. " t.AREAUSERID," +
  321. " w.wrstandardname," +
  322. " w.wrstandardcode," +
  323. " w.unitid," +
  324. " s.todaybuyinqty buyinqty," +
  325. " s.todayselloutqty selloutqty" +
  326. " from RECKON_ERMCP_AREASPOT t" +
  327. " left join tmp s" +
  328. " on t.reckondate = s.reckondate" +
  329. " and t.areauserid = s.userid" +
  330. " and t.wrstandardid = s.wrstandardid" +
  331. " left join wrstandard w" +
  332. " on t.wrstandardid = w.wrstandardid" +
  333. " where 1 = 1"
  334. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  335. if r.WRSTANDARDID > 0 {
  336. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  337. }
  338. if r.RECKONDATE != "" {
  339. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  340. } else if r.BeginDate != "" && r.BeginDate == r.EndDate {
  341. sqlId.And("t.RECKONDATE", r.BeginDate)
  342. } else {
  343. if r.BeginDate != "" {
  344. sqlId.BiggerOrEq("t.RECKONDATE", r.BeginDate)
  345. }
  346. if r.EndDate != "" {
  347. sqlId.LessOrEq("t.RECKONDATE", r.EndDate)
  348. }
  349. }
  350. sqlId.Join(" order by t.RECKONDATE")
  351. return sqlId.String()
  352. }
  353. // GetDataEx 获取现货日报表
  354. func (r *ErmcpReportDaySpot) GetDataEx() (interface{}, error) {
  355. sData := make([]ErmcpReportDaySpot, 0)
  356. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  357. for i := range sData {
  358. sData[i].Calc()
  359. }
  360. return sData, err
  361. }
  362. // ErmcpReportMonSpot 现货月报表
  363. type ErmcpReportMonSpot struct {
  364. CYCLETYPE int32 `json:"cycletype" xorm:"'cycletype'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  365. CYCLETIME string `json:"cycletime" xorm:"'cycletime'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  366. BUYPRICEDQTY float64 `json:"-" xorm:"'BUYPRICEDQTY'"` // 期末采购定价量
  367. SELLPRICEDQTY float64 `json:"-" xorm:"'SELLPRICEDQTY'"` // 期末销售定价量
  368. ORIBUYPRICEDQTY float64 `json:"-" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购定价量
  369. ORISELLPRICEDQTY float64 `json:"-" xorm:"'ORISELLPRICEDQTY'"` // 期初销售定价量
  370. TODAYBUYRECKONQTY float64 `json:"todaybuyreckonqty" xorm:"'TODAYBUYRECKONQTY'"` // 采购确定量
  371. TODAYSELLRECKONQTY float64 `json:"todaysellreckonqty" xorm:"'TODAYSELLRECKONQTY'"` // 销售确定量
  372. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  373. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构T
  374. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  375. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  376. UNITID int32 `json:"-" xorm:"'UNITID'"` // 现货商品单位id
  377. BUYINQTY float64 `json:"buyinqty" xorm:"'BUYINQTY'"` // 采购入库量
  378. SELLOUTQTY float64 `json:"selloutqty" xorm:"'SELLOUTQTY'"` // 销售出库量
  379. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  380. TOTALBUYPRICEDQTY float64 `json:"totalbuypricedqty"` // 采购定价量 = 期末 - 期初
  381. TOTALSELLPRICEDQTY float64 `json:"totalsellpricedqty"` // 销售定价量 = 期末 - 期初
  382. }
  383. // Calc 数据处理
  384. func (r *ErmcpReportMonSpot) Calc() {
  385. r.TOTALBUYPRICEDQTY = r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  386. r.TOTALSELLPRICEDQTY = r.SELLPRICEDQTY - r.ORISELLPRICEDQTY
  387. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  388. }
  389. func (r *ErmcpReportMonSpot) buildSql() string {
  390. var sqlId utils.SQLVal = "with tmp as" +
  391. " (select t.userid," +
  392. " t.wrstandardid," +
  393. " t.cycletime," +
  394. " t.cycletype," +
  395. " sum(t.todaybuyinqty) todaybuyinqty," +
  396. " sum(t.todayselloutqty) todayselloutqty" +
  397. " from Report_ERMCP_AreaStock t" +
  398. " group by t.userid, t.wrstandardid, t.cycletime, t.cycletype)" +
  399. "select t.BUYPRICEDQTY," +
  400. " t.SELLPRICEDQTY," +
  401. " t.Oribuypricedqty," +
  402. " t.Orisellpricedqty," +
  403. " t.TODAYBUYRECKONQTY," +
  404. " t.TODAYSELLRECKONQTY," +
  405. " t.cycletype," +
  406. " t.cycletime," +
  407. " t.WRSTANDARDID," +
  408. " t.AREAUSERID," +
  409. " w.wrstandardname," +
  410. " w.wrstandardcode," +
  411. " w.unitid," +
  412. " s.todaybuyinqty buyinqty," +
  413. " s.todayselloutqty selloutqty" +
  414. " from Report_ERMCP_AreaSpot t" +
  415. " left join tmp s" +
  416. " on t.cycletime = s.cycletime" +
  417. " and t.cycletype = s.cycletype" +
  418. " and t.areauserid = s.userid" +
  419. " and t.wrstandardid = s.wrstandardid" +
  420. " left join wrstandard w" +
  421. " on t.wrstandardid = w.wrstandardid" +
  422. " where t.cycletype = 1"
  423. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  424. sqlId.And("t.cycletime", r.CYCLETIME)
  425. return sqlId.String()
  426. }
  427. // GetDataEx 获取现货月报表
  428. func (r *ErmcpReportMonSpot) GetDataEx() (interface{}, error) {
  429. sData := make([]ErmcpReportMonSpot, 0)
  430. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  431. for i := range sData {
  432. sData[i].Calc()
  433. }
  434. return sData, err
  435. }
  436. // ErmcpReportAreaSpotPL 现货损益日/月表
  437. type ErmcpReportAreaSpotPL struct {
  438. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  439. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  440. SPOTGOODSMODELID int32 `json:"spotgoodsmodelid" xorm:"'SPOTGOODSMODELID'"` // 现货品类ID
  441. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  442. ORIBUYQTY float64 `json:"oribuyqty" xorm:"'ORIBUYQTY'"` // 期初采购总量
  443. ORIBUYAMOUNT float64 `json:"oribuyamount" xorm:"'ORIBUYAMOUNT'"` // 期初采购总额
  444. ORISELLQTY float64 `json:"orisellqty" xorm:"'ORISELLQTY'"` // 期初销售总量
  445. ORISELLAMOUNT float64 `json:"orisellamount" xorm:"'ORISELLAMOUNT'"` // 期初销售总额
  446. ORIQTY float64 `json:"oriqty" xorm:"'ORIQTY'"` // 期初量
  447. ORIAVERAGEPRICE float64 `json:"oriaverageprice" xorm:"'ORIAVERAGEPRICE'"` // 期初均价
  448. ORIAMOUNT float64 `json:"oriamount" xorm:"'ORIAMOUNT'"` // 期初额
  449. TODAYBUYQTY float64 `json:"todaybuyqty" xorm:"'TODAYBUYQTY'"` // 今日采购量(采购增量)
  450. TODAYBUYAMOUNT float64 `json:"todaybuyamount" xorm:"'TODAYBUYAMOUNT'"` // 今日采购额
  451. TODAYBUYAVERAGEPRICE float64 `json:"todaybuyaverageprice" xorm:"'TODAYBUYAVERAGEPRICE'"` // 今日采购均价(采购均价)
  452. TODAYSELLQTY float64 `json:"todaysellqty" xorm:"'TODAYSELLQTY'"` // 今日销售量(销售增量)
  453. TODAYSELLAMOUNT float64 `json:"todaysellamount" xorm:"'TODAYSELLAMOUNT'"` // 今日销售额
  454. TODAYSELLAVERAGEPRICE float64 `json:"todaysellaverageprice" xorm:"'TODAYSELLAVERAGEPRICE'"` // 今日销售均价(销售均价)
  455. CURBUYQTY float64 `json:"curbuyqty" xorm:"'CURBUYQTY'"` // 期末采购总量
  456. CURBUYAMOUNT float64 `json:"curbuyamount" xorm:"'CURBUYAMOUNT'"` // 期末采购总额(采购额)
  457. CURSELLQTY float64 `json:"cursellqty" xorm:"'CURSELLQTY'"` // 期末销售总量
  458. CURSELLAMOUNT float64 `json:"cursellamount" xorm:"'CURSELLAMOUNT'"` // 期末销售总额(销售额)
  459. CURQTY float64 `json:"curqty" xorm:"'CURQTY'"` // 期末量
  460. CURAVERAGEPRICE float64 `json:"curaverageprice" xorm:"'CURAVERAGEPRICE'"` // 期末均价
  461. CURAMOUNT float64 `json:"curamount" xorm:"'CURAMOUNT'"` // 期末额
  462. CURSPOTPRICE float64 `json:"curspotprice" xorm:"'CURSPOTPRICE'"` // 参考市价
  463. CURMARKETVALUE float64 `json:"curmarketvalue" xorm:"'CURMARKETVALUE'"` // 参考市值
  464. ACTUALPL float64 `json:"actualpl" xorm:"'ACTUALPL'"` // 实际损益
  465. FLOATPL float64 `json:"floatpl" xorm:"'FLOATPL'"` // 浮动损益
  466. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  467. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  468. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  469. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  470. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  471. MODELNAME string `json:"modelname" xorm:"'MODELNAME'"` // 品类名称
  472. GBUNITID int32 `json:"gbunitid" xorm:"'GBUNITID'"` // 品类单位id
  473. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  474. GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
  475. ReportType int32 `json:"-"` // 报表类型 1-日报表 2-月报表
  476. ReportDate string `json:"-"` // 格式 日报表(YYYYMMDD) 月报表(YYYYMM)
  477. }
  478. func (r *ErmcpReportAreaSpotPL) calc() {
  479. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  480. r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GBUNITID)
  481. }
  482. func (r *ErmcpReportAreaSpotPL) buildSql() string {
  483. var sqlId utils.SQLVal = "SELECT t.AREAUSERID," +
  484. " t.WRSTANDARDID," +
  485. " t.SPOTGOODSMODELID," +
  486. " t.SPOTGOODSBRANDID," +
  487. " t.ORIBUYQTY," +
  488. " t.ORIBUYAMOUNT," +
  489. " t.ORISELLQTY," +
  490. " t.ORISELLAMOUNT," +
  491. " t.ORIQTY," +
  492. " t.ORIAVERAGEPRICE," +
  493. " t.ORIAMOUNT," +
  494. " t.TODAYBUYQTY," +
  495. " t.TODAYBUYAMOUNT," +
  496. " t.TODAYBUYAVERAGEPRICE," +
  497. " t.TODAYSELLQTY," +
  498. " t.TODAYSELLAMOUNT," +
  499. " t.TODAYSELLAVERAGEPRICE," +
  500. " t.CURBUYQTY," +
  501. " t.CURBUYAMOUNT," +
  502. " t.CURSELLQTY," +
  503. " t.CURSELLAMOUNT," +
  504. " t.CURQTY," +
  505. " t.CURAVERAGEPRICE," +
  506. " t.CURAMOUNT," +
  507. " t.CURSPOTPRICE," +
  508. " t.CURMARKETVALUE," +
  509. " t.ACTUALPL," +
  510. " t.FLOATPL," +
  511. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  512. " w.wrstandardcode," +
  513. " w.wrstandardname," +
  514. " w.unitid," +
  515. " gb.brandname," +
  516. " gm.modelname," +
  517. " gm.unitid gbunitid" +
  518. " FROM %v t" +
  519. " left join wrstandard w" +
  520. " on t.wrstandardid = w.wrstandardid" +
  521. " left join spotgoodsbrand gb" +
  522. " on t.spotgoodsbrandid = gb.brandid" +
  523. " left join spotgoodsmodel gm" +
  524. " on t.spotgoodsmodelid = gm.modelid" +
  525. " WHERE 1 = 1"
  526. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  527. if r.ReportType == 1 {
  528. // 日报表
  529. sqlId.FormatParam("RECKON_ERMCP_AREASPOTPL")
  530. sqlId.And("t.reckondate", r.ReportDate)
  531. } else {
  532. // 月报表
  533. sqlId.FormatParam("REPORT_ERMCP_AREASPOTPL")
  534. sqlId.And("t.cycletype", 1)
  535. sqlId.And("t.cycletime", r.ReportDate)
  536. }
  537. if r.WRSTANDARDID > 0 {
  538. sqlId.And("t.wrstandardid", r.WRSTANDARDID)
  539. }
  540. if r.SPOTGOODSBRANDID > 0 {
  541. sqlId.And("t.spotgoodsbrandid", r.SPOTGOODSBRANDID)
  542. }
  543. if r.SPOTGOODSMODELID > 0 {
  544. sqlId.And("t.spotgoodsmodelid", r.SPOTGOODSMODELID)
  545. }
  546. return sqlId.String()
  547. }
  548. // GetDataEx 获取现货损益日(月)报表
  549. func (r *ErmcpReportAreaSpotPL) GetDataEx() (interface{}, error) {
  550. sData := make([]ErmcpReportAreaSpotPL, 0)
  551. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  552. for i := range sData {
  553. sData[i].calc()
  554. }
  555. return sData, err
  556. }