package com.kmall.admin.controller.mk.store;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.kmall.admin.entity.OrderEntity;
import com.kmall.admin.entity.mk.store.MkStorePromStatEntity;
import com.kmall.admin.service.OrderService;
import com.kmall.admin.service.mk.store.MkStorePromStatService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.StringUtils;
import com.kmall.common.utils.excel.ExcelExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;

import javax.servlet.http.HttpServletResponse;

/**
 * 门店推广统计Controller
 *
 * @author huangyq
 * @email admin@qhdswl.com
 * @date 2019-04-26 15:52:20
 */
@Controller
@RequestMapping("mkstorepromstat")
public class MkStorePromStatController {
    @Autowired
    private MkStorePromStatService mkStorePromStatService;

    @Autowired
    private OrderService orderService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mkstorepromstat:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        //查询列表数据
        Query query = new Query(params);

        List<MkStorePromStatEntity> mkStorePromStatList = mkStorePromStatService.queryList(query);
        int total = mkStorePromStatService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(mkStorePromStatList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{promStatId}")
    @RequiresPermissions("mkstorepromstat:info")
    @ResponseBody
    public R info(@PathVariable("promStatId") Integer promStatId) {
        MkStorePromStatEntity mkStorePromStat = mkStorePromStatService.queryObject(promStatId);

        return R.ok().put("mkStorePromStat", mkStorePromStat);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkstorepromstat:save")
    @ResponseBody
    public R save(@RequestBody MkStorePromStatEntity mkStorePromStat) {
        mkStorePromStatService.save(mkStorePromStat);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkstorepromstat:update")
    @ResponseBody
    public R update(@RequestBody MkStorePromStatEntity mkStorePromStat) {
        mkStorePromStatService.update(mkStorePromStat);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mkstorepromstat:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]promStatIds) {
        mkStorePromStatService.deleteBatch(promStatIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");

        List<MkStorePromStatEntity> list = mkStorePromStatService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/promOrderList")
//    @RequiresPermissions("mkstorepromstat:promOrderList")
    @ResponseBody
    public R promOrderList(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        //查询列表数据
        Query query = new Query(params);

        List<OrderEntity> mkStorePromStatList = orderService.promOrderList(query);
        int total = orderService.queryPromOrderTotal(query);

        PageUtils pageUtil = new PageUtils(mkStorePromStatList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/exportPromOrderList")
//    @RequiresPermissions("mkstorepromstat:exportPromOrderList")
    public R exportPromOrderList(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");

        List<OrderEntity> orderEntityList = orderService.promOrderListExport(params);

        ExcelExport ee = new ExcelExport("渠道追踪推广订单列表");

        String[] header = new String[]{"推广id", "订单编号", "商品", "商品单价", "订单实付金额", "渠道推广实收金额", "购买数量",  "会员", "商户订单号", "优惠券使用", "运费", "订单状态", "付款状态"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (orderEntityList != null && orderEntityList.size() != 0) {
            for (OrderEntity orderEntity : orderEntityList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("PromId", orderEntity.getPromId());
                map.put("OrderSn", orderEntity.getOrderSn());
                map.put("GoodsName", orderEntity.getGoodsName());
                map.put("RetailPrice", orderEntity.getRetailPrice());
                map.put("ActualPrice", orderEntity.getActualPrice());
                map.put("promActualPrice", orderEntity.getPromActualPrice());
                map.put("Number", orderEntity.getNumber());
                map.put("UserName", orderEntity.getUserName());
                map.put("MerchOrderSn", orderEntity.getMerchOrderSn());
                map.put("ActivityId", orderEntity.getActivityId()==null?"未使用":orderEntity.getActivityId());
                map.put("FreightPrice", orderEntity.getFreightPrice());
                map.put("OrderStatus", StringUtils.isNotEmpty(String.valueOf(orderEntity.getOrderStatus()))?
                        Dict.orderStatus.valueOf("item_"+orderEntity.getOrderStatus()).getItemName():"");
                map.put("PayStatus", StringUtils.isNotEmpty(String.valueOf(orderEntity.getPayStatus()))?
                        Dict.payStatus.valueOf("item_"+orderEntity.getPayStatus()).getItemName():"");
                list.add(map);
            }
        }

        ee.addSheetByMap("渠道追踪推广订单列表", list, header);
        ee.export(response);
        return R.ok();
    }

}
