package org.jeecg.modules.userCode.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.tbOrder.entity.TbOrder;
import org.jeecg.modules.tbOrder.service.ITbOrderService;
import org.jeecg.modules.tbTask.entity.TbTask;
import org.jeecg.modules.tbTask.service.ITbTaskService;
import org.jeecg.modules.userCode.model.req.TaskReq;
import org.jeecg.modules.userCode.model.req.TaskReq3;
import org.jeecg.modules.userCode.service.AppletLoginService;
import org.jeecg.modules.userCode.service.OrderService;
import org.jeecg.modules.userCode.service.UserService;
import org.jeecg.modules.userRole.entity.TbUserRole;
import org.jeecg.modules.utils.ValidateTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * 服务化 system模块 对外接口请求类
 *
 * @author: jeecg-boot
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单相关接口")
public class OrderController {

    @Resource
    private ITbOrderService tbOrderService;
    @Resource
    private OrderService orderService;
    @Resource
    private ShiroRealm shiroRealm;


    @ApiOperation(value = "发起聘请，等待师傅确认", notes = "发起聘请，等待师傅确认")
    @PostMapping(value = "/addTask")
    public Result<Map> addOrder(@RequestHeader("X-Access-Token") String token, @RequestBody TaskReq3 taskReq3) {
        if (ValidateTool.isNull(taskReq3.getTaskId())) {
            throw new JeecgBootException("参数错误，请选择师傅");
        }
        String orderId = orderService.addOrder(taskReq3.getTaskId(), token);
        Map<String, Object> map = new HashMap<>();
        map.put("orderId", orderId);
        return Result.OK(map);
    }


    /**
     * 通过id查询订单详情
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "tb_user_role-通过id查询")
    @ApiOperation(value = "通过id查询订单详情", notes = "通过id查询订单详情")
    @GetMapping(value = "/queryById")
    public Result<TbOrder> queryById(@RequestParam(name = "orderId", required = true) String orderId) {
        TbOrder byId = tbOrderService.getById(orderId);
        if (byId == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(byId);
    }


    @ApiOperation(value = "招聘方订单分页列表查询", notes = "招聘方订单分页列表查询")
    @GetMapping(value = "/bossOrderList")
    public Result<IPage<TbOrder>> orderList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestHeader("X-Access-Token") String token,
                                            @RequestParam(name = "orderStatus", defaultValue = "0") Integer orderStatus
    ) {

        //订单状态 0待聘用 1 已接单 2 进行中 3试工完成 4 企业确认 5已支付 6已完成 7已取消
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        QueryWrapper<TbOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("boss_id", hanHaiMember.getId());
        //orderStatus==0 全部订单
        Page<TbOrder> page = new Page<TbOrder>(pageNo, pageSize);
        IPage<TbOrder> pageList = tbOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @ApiOperation(value = "求职方订单分页列表查询", notes = "求职方订单分页列表查询")
    @GetMapping(value = "/workOrderList")
    public Result<IPage<TbOrder>> workOrderList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestHeader("X-Access-Token") String token,
                                            @RequestParam(name = "orderStatus", defaultValue = "0") Integer orderStatus
    ) {

        //订单状态 0待聘用 1 已接单 2 进行中 3试工完成 4 企业确认 5已支付 6已完成 7已取消
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        QueryWrapper<TbOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", hanHaiMember.getId());
        //orderStatus==0 全部订单
        Page<TbOrder> page = new Page<TbOrder>(pageNo, pageSize);
        IPage<TbOrder> pageList = tbOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }



}
