package cn.pluss.platform.controller.refundOrder;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.MerchantRefundOrder;
import cn.pluss.platform.refundOrder.MerchantRefundOrderService;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiredArgsConstructor
@RequestMapping("refundOrder")
public class MerchantRefundOrderController extends BaseNoModelController {

    private final MerchantRefundOrderService merchantRefundOrderService;

    private Integer PAGE_SIZE = 12;

    @GetMapping("index")
    public String index() {
        return "refundOrder/refundOrderList";
    }

    @RequestMapping(value = "/queryRefundOrderByPage", method = RequestMethod.POST)
    @ResponseBody
    public Result<Page<MerchantRefundOrder>> queryRefundOrderByPage(Page<MerchantRefundOrder> page,
                                                                    MerchantRefundOrder refundOrder) {
        Boolean bol = verifyUserRole();
        ShiroUser shiroUser = queryCurrentShiroUser();
        if (!bol) {
            refundOrder.setMerchantCode(shiroUser.getMerchantCode());
        }
        if(StringUtil.isNotEmpty(refundOrder.getPayTypeCode()) && refundOrder.getPayTypeCode().contains("|")){
            List<String> payTypeCodeList = Arrays.asList(refundOrder.getPayTypeCode().split("\\|"));
            refundOrder.setPayTypeCodeList(payTypeCodeList);
        }
        Page<MerchantRefundOrder> pageDate = merchantRefundOrderService.pageDate(page, refundOrder);
        return ResultGenerator.genSuccessResult(pageDate);
    }

    @GetMapping("marketIndex")
    public String marketIndex() {
        return "refundOrder/marketOrderList";
    }

    @RequestMapping(value = "/queryMarketCountByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMarketCountByPage(@RequestParam(defaultValue = "1") Integer currPage, MerchantRefundOrder refundOrder) {
        Map<String, Object> map = new HashMap<>(16);
        Map<String, Object> result = new HashMap<>(16);
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        if(StringUtil.isNotEmpty(refundOrder.getAlias())){
            map.put("alias",refundOrder.getAlias());
        }
        //管理员查看的数据
        List<MerchantRefundOrder> refundOrderCountList = merchantRefundOrderService.queryRefundOrderJoinPage(map);
        Integer count = merchantRefundOrderService.queryRefundOrderJoinPageCount(map);
        result.put("refundOrderCountList", refundOrderCountList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

}
