package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.Order;
import com.winsun.mapper.AppMapper;
import com.winsun.mapper.OrderMapper;
import com.winsun.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/hhrUser")
public class hhrUserController extends BaseController {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private AppMapper appMapper;

    /**
     * 查询订单列表
     *
     * @param userId      用户id
     * @param pageNo      查询页码 默认首页
     * @param pageSize    查询每页数量 默认10
     * @param customer    查询条件-联系号码或联系人
     * @param orderStatus 查询条件-订单状态
     * @param userType    查询条件-用户类型
     * @param selectFlag  查询内容-0：个人   1：督导   默认0
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getOrderList", method = RequestMethod.POST)
    public ResponseData<Map<String, Object>> getOrderList(String userId, Integer pageNo, Integer pageSize, String customer, String orderStatus, String userType, Integer selectFlag) {
        if (StringUtils.isBlank(userId)) {
            return ResponseData.error("userId为空");
        }
        Map<String, Object> resultMap = new HashMap<>();

        List<String> userList = new ArrayList<>();
        if (selectFlag == null || selectFlag == 0) {
            userList.add(userId);
        } else {
            List<String> managerSchoolList = appMapper.selectManagerSchool(userId);
            if (managerSchoolList == null || managerSchoolList.size() == 0) {
                return ResponseData.success(resultMap, "该用户无负责的学校");
            }
            List<String> memberList = appMapper.selectUserBySchool(managerSchoolList);
            userList.addAll(memberList);
        }

        Page<Order> page = new Page<>(pageNo == null ? 1 : pageNo, pageSize == null ? 10 : pageSize);
        Wrapper<Order> wrapper = new EntityWrapper();
        wrapper.in("hehuoren_id", userList);
        if (StringUtils.isNotBlank(customer)) {
            wrapper.addFilter("(contact_number LIKE '%" + customer + "%' OR customer_name LIKE '%" + customer + "%')");
        }
        wrapper.eq(StringUtils.isNotBlank(orderStatus), "order_status", orderStatus);
        wrapper.eq(StringUtils.isNotBlank(userType), "user_type", userType);
        wrapper.orderBy("create_time",false);

        List<Order> orderList = orderMapper.selectPage(page, wrapper);
        page.setRecords(orderList);
        resultMap.put("orderList", orderList);

        Wrapper<Order> numWrapper = wrapper.clone();
        numWrapper.setSqlSelect("COUNT(CASE WHEN order_status IN ('异常单','重新下单') THEN 1 END)AS errorNum," +
                "COUNT(CASE WHEN order_status IN ('审核中','待受理','待处理','待活体','待派奖','待选号','待配送','提交中') THEN 1 END)AS dealNum");
        List<Map<String, Object>> numList = orderMapper.selectMaps(numWrapper);
        resultMap.put("orderStatusNum", numList.get(0));

        return ResponseData.success(resultMap, "查询成功");
    }
}
