package cn.superdesk.uniorder.controller.score;

import cn.superdesk.uniorder.common.excel.ExcelResponse;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderGoodsDetailVo;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedOrderGoodsVo;
import cn.superdesk.uniorder.dubbo.consumer.order.base.UnifiedOrderDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.InvoiceApplyCustomerDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import cn.superdesk.uniorder.common.excel.ViewUnifiedOrderGoodsUtils;

/**
 * @Description: 侨城汇 积分订单 王思远的原型上已经没有这个了 订单管理
 * @Author: linjinyu
 * @CreateDate: 6/12/20 11:49 AM
 * @UpdateUser: linjinyu
 * @UpdateDate: 6/12/20 11:49 AM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Deprecated
@Slf4j
@RestController
@RequestMapping("/order/score")
public class ScoreOrderController extends BaseController {

	@Autowired
	private UnifiedOrderDubboConsumerService         unifiedOrderDubboConsumerService;
	@Autowired
	private InvoiceApplyCustomerDubboConsumerService invoiceApplyDubboConsumerService;

	/**
	 * 订单管理>订单管理(企业采购) opt 导出成本
	 *
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getcostpriceexcel")
	public void getCostPriceExcel(HttpServletResponse response) throws IOException {
		try {

			// 前端过来的参数
			Map<String, Object> params = new HashMap<>();

			params.put("sn", getString("sn"));
			params.put("virtId", getString("virtId"));
			params.put("virtName", getString("virtName"));
			params.put("supId", getString("supId"));
			params.put("supName", getString("supName"));
			params.put("userId", getString("userId"));
			params.put("orderType", getString("orderType"));
			params.put("statename", getString("statename"));
			params.put("username", getString("username"));
			params.put("userphone", getString("userphone"));
			params.put("paymentType", getString("paymentType"));
			params.put("startime", getString("startime"));
			params.put("endtime", getString("endtime"));

			// 查询数据
			List<UnifiedOrderGoodsVo> dataList = unifiedOrderDubboConsumerService.getOrderManagerGoodsView(getToken(), params);

			for (int i = 0; i < dataList.size(); i++) {

				UnifiedOrderGoodsVo item = dataList.get(i);

				item.setGoodsAmount(new BigDecimal(item.getPrice().doubleValue() * item.getQuantity()));

//				ViewUnifiedOrderGoodsUtils.setInvStateName(item);
//				ViewUnifiedOrderGoodsUtils.setPayTypeName(item);

				// 临时 查找发票信息
				Map<String, Object> invQuery = new HashMap<>();
				invQuery.put("sn", item.getOrderSn());
				List<InvoiceApply> applies = invoiceApplyDubboConsumerService.findInvoiceByOrderSn(invQuery);

				if (applies != null && applies.size() == 1) {
					item.setInvoiceNumber(applies.get(0).getInvoiceNumber());
					item.setInvoiceApplySn(applies.get(0).getInvoiceApplySn());
					item.setInvoiceStatusName("已开票");
				}

			}

			String[] field = {"orderSn", "projectName", "purchaseVirtName", "amount", "payTypeName", "orderStateName",
					"purchaseUserName",
					"purchasePhone",
					"recAddress", "createTime", "saleName", "remarks", "", "goodsName", "quantity", "unit", "price", "costPrice",
					"goodsAmount", "recUserName", "recUserPhone", "updateTime", "updateTime", "", "invoiceNumber", "", "invoiceStatusName", "invoiceApplySn"
			};

			String[] fieldZhCn = {"订单编号", "会员所属项目", "会员所属企业(动态)", "订单金额", "支付方式", "订单状态",
					"下单人姓名",
					"下单人电话",
					"收货地址", "下单时间", "供应商名称", "买家备注", "卖家备注", "商品信息", "商品数量", "商品单位", "商品单价(折扣后)", "成本价格",
					"商品总价(折扣后)", "收货人姓名", "收货人手机号", "发货时间", "完成时间", "回款状态", "发票号", "开票时间", "发票状态", "开票申请编号"};

			ExcelResponse.out(dataList, fieldZhCn, field, "订单(商品)", "订单管理" + "_" + "导出成本" + "_" + "", response);


		} catch (Exception e) {

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
	}

	/***
	 * 订单详情
	 * @return
	 */
	@PostMapping("/detail")
	@ResponseBody
	public R<UnifiedOrderGoodsDetailVo> getDetail(Long id) {

		R<UnifiedOrderGoodsDetailVo> result = new R<>();
		try {

			UnifiedOrderGoodsDetailVo detail = unifiedOrderDubboConsumerService.getGoodsDetail(id, getToken());
			result.setData(detail);

		} catch (Exception e) {

			result.setCode(String.valueOf(e.hashCode()));
			result.setSuccess(false);
			result.setMessage(e.getMessage());

			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}
}
