package com.ruoyi.app.mendian;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.app.mendian.dto.ExportInput;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PosOrder;
import com.ruoyi.system.mapper.PosOrderMapper;
import com.ruoyi.system.mapper.PosStoreMapper;
import com.ruoyi.system.mapper.UserBillingMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/index")
public class ShindexController extends BaseController {
    @Autowired  //商品分类
    private IPosFenleiService posFenleiService;
    @Autowired  //商品
    private IPosFoodService posFoodService;
    @Autowired  //门店
    private IPosStoreService posStoreService;
    @Autowired  //门店
    private PosStoreMapper posStoreMapper;
    @Autowired  //订单
    private PosOrderMapper posOrderMapper;
    @Autowired  //账单
    private UserBillingMapper userBillingMapper;
    @Autowired
    private IPosOrderService posOrderService;
    @Autowired
    private IInfoUserService infoUserService;
    @Autowired
    private IInfoAddressService infoAddressService;

    @Anonymous
    @GetMapping("/getshindex")
    public AjaxResult getshindex(@RequestParam String id)
    {
        JSONObject org = new JSONObject();
        int mdsum = posStoreMapper.getshstore(Long.valueOf(id));
        int dayorder = posOrderMapper.getshoredr(Long.valueOf(id));
        int qbuorder = posOrderMapper.getshqbuord(Long.valueOf(id));
        int qbshour = userBillingMapper.getshqbsum(Long.valueOf(id));
        int qbtixian = userBillingMapper.getshtxsum(Long.valueOf(id));
        int dtshour = userBillingMapper.getshdtsum(Long.valueOf(id));
        org.put("stroe",mdsum);
        org.put("dtorder",dayorder);
        org.put("qborder",qbuorder);
        org.put("qbshour",qbshour);
        org.put("dtshour",dtshour);
        org.put("ketixian",qbshour-qbtixian);
        return success(org);
    }

    //查询商家的订单
    @Anonymous
    @Auth
    @GetMapping("/getstoreorderlist")
    public AjaxResult getstoreorderlist(@RequestHeader String token,
                                        @RequestParam Integer page,
                                        @RequestParam Integer size,
                                        @RequestParam String mdId,
                                        @RequestParam(defaultValue = "") String state,
                                        @RequestParam(defaultValue = "") String diningStatus,
                                        @RequestParam(defaultValue = "") String startTime,
                                        @RequestParam(defaultValue = "") String endTime)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        IPage<PosOrder> palist = new Page<>(page,size);
        QueryWrapper<PosOrder> queryWrapper= new QueryWrapper<>();
        queryWrapper.select().orderByDesc("cretim");
        queryWrapper.eq("sh_id",id);
        System.out.println("商户id:"+id);
        queryWrapper.eq("md_id",mdId);
        if(!"".equals(state)){
            if(state.equals("z01")){
                queryWrapper.eq("state",0).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("collect_payment","1")
                ).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("type",1)
                ).or().eq("state",1).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("sh_id",id)
                ).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("md_id",mdId)
                );
            }else if(state.equals("z234")){
                queryWrapper.in("state",2,3,4);
            }else if(state.equals("z23")){
                queryWrapper.in("state",2,3);
            }else if(state.equals("z34")){
                queryWrapper.in("state",3,4);
            }else if(state.equals("z6789")){
                queryWrapper.in("state",6,7,8,9);
            }else{
                queryWrapper.eq("state",state);
            }
        }
        if(!"".equals(diningStatus)){
            queryWrapper.eq("dining_status",diningStatus);
        }
        if(!"".equals(startTime)){
            queryWrapper.ge("cretim",startTime);
        }
        if(!"".equals(endTime)){
            queryWrapper.le("cretim",endTime);
        }
        IPage<PosOrder> list =  posOrderService.page(palist,queryWrapper);
        List<PosOrder> orlist = list.getRecords();
        JSONArray arr = new JSONArray();
        JSONObject jec = new JSONObject();
        for(int i=0;i<orlist.size();i++){
            JSONObject org = new JSONObject();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            org.put("id",orlist.get(i).getId());
            org.put("ddId",orlist.get(i).getDdId());
            org.put("shanghu",infoUserService.getById(orlist.get(i).getShId()));
            org.put("qishou",infoUserService.getById(orlist.get(i).getQsId()));
            org.put("store",posStoreService.getById(orlist.get(i).getMdId()));
            org.put("cretim",sdf.format(orlist.get(i).getCretim()));
            org.put("shaddress",infoAddressService.getById(orlist.get(i).getShdzId()));
            org.put("user",infoUserService.getById(orlist.get(i).getUserId()));
            org.put("amount",orlist.get(i).getAmount());
            org.put("remarks",orlist.get(i).getRemarks());
            org.put("state",orlist.get(i).getState());
            org.put("type",orlist.get(i).getType());
            org.put("jvli",orlist.get(i).getJvli());
            org.put("freight",orlist.get(i).getFreight());
            org.put("delryTime",orlist.get(i).getDelryTime());
            org.put("diningStatus",orlist.get(i).getDiningStatus());
            org.put("collectPayment",orlist.get(i).getCollectPayment());
            org.put("food",JSONArray.parseArray(orlist.get(i).getFood()));
            org.put("activity",orlist.get(i).getActivity());
            org.put("mdActivity",orlist.get(i).getMdActivity());
            org.put("salesReduction",orlist.get(i).getSalesReduction());
            org.put("mdSalesReduction",orlist.get(i).getMdSalesReduction());
            org.put("salesName",orlist.get(i).getSalesName());
            org.put("mdSalesName",orlist.get(i).getMdSalesName());
            org.put("mdDiscountAmount",orlist.get(i).getMdDiscountAmount());
            org.put("discountAmount",orlist.get(i).getDiscountAmount());
            org.put("mdYhId",orlist.get(i).getMdYhId());
            org.put("mdYhName",orlist.get(i).getMdYhName());
            org.put("yhId",orlist.get(i).getYhId());
            org.put("yhName",orlist.get(i).getYhName());
            org.put("points",orlist.get(i).getPoints());
            org.put("pointsReduction",orlist.get(i).getPointsReduction());
            arr.add(org);
        }
        jec.put("page",list.getPages());
        jec.put("size",list.getSize());
        jec.put("total",list.getTotal());
        jec.put("list",arr);
        return success(jec);
    }
    @Anonymous
    @Auth
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestHeader String token, @RequestBody ExportInput input){
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        QueryWrapper<PosOrder> queryWrapper= new QueryWrapper<>();
        queryWrapper.select().orderByDesc("cretim");
        queryWrapper.eq("sh_id",id);
        System.out.println("商户id:"+id);
        queryWrapper.eq("md_id",input.getMdId());
        if(!"".equals(input.getState())){
            if(input.getState().equals("z01")){
                queryWrapper.eq("state",0).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("collect_payment","1")
                ).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("type",1)
                ).or().eq("state",1).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("sh_id",id)
                ).and(
                        posOrderQueryWrapper -> posOrderQueryWrapper.eq("md_id",input.getMdId())
                );
            }else if(input.getState().equals("z234")){
                queryWrapper.in("state",2,3,4);
            }else if(input.getState().equals("z23")){
                queryWrapper.in("state",2,3);
            }else if(input.getState().equals("z34")){
                queryWrapper.in("state",3,4);
            }else if(input.getState().equals("z6789")){
                queryWrapper.in("state",6,7,8,9);
            }else{
                queryWrapper.eq("state",input.getState());
            }
        }
        if(!"".equals(input.getDiningStatus())){
            queryWrapper.eq("dining_status",input.getDiningStatus());
        }
        if(!"".equals(input.getStartTime())){
            queryWrapper.ge("cretim",input.getStartTime());
        }
        if(!"".equals(input.getEndTime())){
            queryWrapper.le("cretim",input.getEndTime());
        }
        List<PosOrder> list =  posOrderService.list(queryWrapper);
        ExcelUtil<PosOrder> util = new ExcelUtil<PosOrder>(PosOrder.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.posorder"));
    }

}
