package com.yunniu.farming.webadmin.controller;

import com.yunniu.farming.common.plugin.PageInfo;
import com.yunniu.farming.result.Result;
import com.yunniu.farming.webadmin.model.GroupOrder;
import com.yunniu.farming.webadmin.model.OrderMain;
import com.yunniu.farming.webadmin.model.OrderSub;
import com.yunniu.farming.webadmin.service.GroupOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @author zyf
 * @date 2023/5/16
 * @apiNote
 */

@Controller
@RequestMapping("/groupOrder")
public class GroupOrderController {


    @Autowired
    private GroupOrderService groupOrderService;

    @RequestMapping("/list")
    public String list(Model model) {
        return "groupOrder/list";
    }

    @RequestMapping("/detailList")
    public String detailList(Model model, Long id) {
        model.addAttribute("orderId", id);
        return "groupOrder/detailList";
    }

    @RequestMapping(value = "/orderDetail")
    public String addOrEdit(Model model, Long id) {
        model.addAttribute("id", id);
        if (id == null || id.equals("")) {
            model.addAttribute("obj", new GroupOrder());
        } else {
            GroupOrder order = groupOrderService.findById(id);
            model.addAttribute("obj", order);
        }
        return "groupOrder/detail";
    }

    /**
     * 发货
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateFwStatus", method = RequestMethod.GET)
    @ResponseBody
    public Result updateFwStatus(Long id, String remark) {
        return Result.success(groupOrderService.sendOutGoods(id, remark));
    }

    /**
     * 完成
     * @param id
     * @return
     */
    @RequestMapping(value = "/successOrder")
    @ResponseBody
    public Result successOrder(Long id) {
        return Result.success(groupOrderService.successOrder(id));
    }

    @RequestMapping(value = "/findPageList", method = RequestMethod.POST)
    @ResponseBody
    public Result findPageList(@RequestBody GroupOrder item) {
        //这里对返回对象用Map处理，map.pageinfo=>分页信息；map.items=>结果数据集；
        Map<String,Object> rmap = new HashMap<String,Object>(2);

        //传入当前页数，适配PC端Layui分页
        if (item.getCurpage() != null || item.getPagesize() != null) {
            PageInfo pinfo = new PageInfo();

            if (item.getCurpage() != null) {
                pinfo.setCurPage(item.getCurpage());
            }
            if (item.getPagesize() != null) {
                pinfo.setPageSize(item.getPagesize());
            }
            item.setPageinfo(pinfo);
        }
        // 1.查询前 需要对分页对象 做处理，主要是 分页 开始记录数 limit arg0开始记录，arg1每页几条记录
        PageInfo pinfo = item.getPageinfo();
        if (pinfo == null) {
            pinfo = new PageInfo();
            item.setPageinfo(pinfo);
        } else {
            // 分页开始记录数
            int curRecord = (pinfo.getCurPage() - 1) * pinfo.getPageSize();
            pinfo.setCurRecord(curRecord);
            item.setPageinfo(pinfo);
        }

        List<GroupOrder> list = groupOrderService.findPageList(item);

        // 3.将查询结果的 分页数据封装后返回
        int totalRs = pinfo.getTotalRecords();        //总记录数
        int totalPs = 0;                            //总页数
        if (totalRs % pinfo.getPageSize() == 0) {        //总页数计算
            totalPs = totalRs / pinfo.getPageSize();
        } else {
            totalPs = 1 + totalRs / pinfo.getPageSize();
        }
        pinfo.setTotalPages(totalPs);

        // 4.将分页对象、结果集合 封装后返回前台
        rmap.put("pageinfo", pinfo);
        rmap.put("items", list);
        return Result.success(rmap);
    }

    /**
     * 团购订单用户列表
     * @param item 团购订单id
     * @return
     */
    @RequestMapping(value = "/userList",method = RequestMethod.POST)
    @ResponseBody
    public Result userList(@RequestBody OrderMain item){
        //这里对返回对象用Map处理，map.pageinfo=>分页信息；map.items=>结果数据集；
        Map<String,Object> rmap = new HashMap<String,Object>(2);

        //传入当前页数，适配PC端Layui分页
        if (item.getCurpage() != null || item.getPagesize() != null) {
            PageInfo pinfo = new PageInfo();

            if (item.getCurpage() != null) {
                pinfo.setCurPage(item.getCurpage());
            }
            if (item.getPagesize() != null) {
                pinfo.setPageSize(item.getPagesize());
            }
            item.setPageinfo(pinfo);
        }
        // 1.查询前 需要对分页对象 做处理，主要是 分页 开始记录数 limit arg0开始记录，arg1每页几条记录
        PageInfo pinfo = item.getPageinfo();
        if (pinfo == null) {
            pinfo = new PageInfo();
            item.setPageinfo(pinfo);
        } else {
            // 分页开始记录数
            int curRecord = (pinfo.getCurPage() - 1) * pinfo.getPageSize();
            pinfo.setCurRecord(curRecord);
            item.setPageinfo(pinfo);
        }

        List<OrderMain> list = groupOrderService.userList(item);

        // 3.将查询结果的 分页数据封装后返回
        int totalRs = pinfo.getTotalRecords();        //总记录数
        int totalPs = 0;                            //总页数
        if (totalRs % pinfo.getPageSize() == 0) {        //总页数计算
            totalPs = totalRs / pinfo.getPageSize();
        } else {
            totalPs = 1 + totalRs / pinfo.getPageSize();
        }
        pinfo.setTotalPages(totalPs);

        // 4.将分页对象、结果集合 封装后返回前台
        rmap.put("pageinfo", pinfo);
        rmap.put("items", list);
        return Result.success(rmap);
    }

    /**
     * 购买商品列表
     * @param item 用户列表id
     * @return
     */
    @RequestMapping(value = "/productList",method = RequestMethod.POST)
    @ResponseBody
    public Result productList(@RequestBody OrderSub item){
//这里对返回对象用Map处理，map.pageinfo=>分页信息；map.items=>结果数据集；
        Map<String,Object> rmap = new HashMap<String,Object>(2);

        //传入当前页数，适配PC端Layui分页
        if (item.getCurpage() != null || item.getPagesize() != null) {
            PageInfo pinfo = new PageInfo();

            if (item.getCurpage() != null) {
                pinfo.setCurPage(item.getCurpage());
            }
            if (item.getPagesize() != null) {
                pinfo.setPageSize(item.getPagesize());
            }
            item.setPageinfo(pinfo);
        }
        // 1.查询前 需要对分页对象 做处理，主要是 分页 开始记录数 limit arg0开始记录，arg1每页几条记录
        PageInfo pinfo = item.getPageinfo();
        if (pinfo == null) {
            pinfo = new PageInfo();
            item.setPageinfo(pinfo);
        } else {
            // 分页开始记录数
            int curRecord = (pinfo.getCurPage() - 1) * pinfo.getPageSize();
            pinfo.setCurRecord(curRecord);
            item.setPageinfo(pinfo);
        }
        List<OrderSub> list = groupOrderService.productList(item);
        // 3.将查询结果的 分页数据封装后返回
        int totalRs = pinfo.getTotalRecords();        //总记录数
        int totalPs = 0;                            //总页数
        if (totalRs % pinfo.getPageSize() == 0) {        //总页数计算
            totalPs = totalRs / pinfo.getPageSize();
        } else {
            totalPs = 1 + totalRs / pinfo.getPageSize();
        }
        pinfo.setTotalPages(totalPs);

        // 4.将分页对象、结果集合 封装后返回前台
        rmap.put("pageinfo", pinfo);
        rmap.put("items", list);
        return Result.success(rmap);
    }
}
