package io.hmit.modules.serviceOrder.controller;

import io.hmit.annotation.Login;
import io.hmit.annotation.LoginUser;
import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData;
import io.hmit.common.utils.Result;
import io.hmit.common.validator.AssertUtils;
import io.hmit.common.validator.ValidatorUtils;
import io.hmit.common.validator.group.AddGroup;
import io.hmit.common.validator.group.DefaultGroup;
import io.hmit.common.validator.group.UpdateGroup;
import io.hmit.entity.UserEntity;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.modules.serviceOrder.dto.ReservationDTO;
import io.hmit.modules.serviceOrder.service.PensionOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.Map;

/**
 * 订单表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@RestController
@RequestMapping("serviceOrder/pensionorder")
@Api(tags="订单表")
public class PensionOrderController {
    @Autowired
    private PensionOrderService pensionOrderService;

    @Login
    @GetMapping("page")
    @ApiOperation("全部订单(志愿者)")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<PensionOrderDTO>> page(@ApiIgnore @LoginUser UserEntity user,
                                                  @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getAllOrderPage(params, user.getId());
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("getElderAllOrderOrByStatusList")
    @ApiOperation("全部/根据状态返回订单(老人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = "status", value = "订单状态： 0未接单 1已接单 2已完成 3已取消", paramType = "query", dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<PensionOrderDTO>> getElderAllOrderOrByStatusList(@ApiIgnore @LoginUser UserEntity creator,
                                                                            @ApiIgnore @RequestParam Map<String, Object> params){
        Integer status = null;
        if (null != params.get("status")) {
            status = Integer.parseInt(params.get("status").toString());
        }
        PageData<PensionOrderDTO> page = pensionOrderService.getElderAllOrderOrByStatusPage(params, creator.getId(), status);
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("getOrderPageByStatus")
    @ApiOperation(value = "根据状态返回订单(志愿者)", notes = "不能查看未接单订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = "status", value = "订单状态： 0未接单 1已接单 2已完成 3已取消", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<PensionOrderDTO>> getOrderPageByStatus(@ApiIgnore @LoginUser UserEntity user,
                                                                  @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getOrderPageByStatus(params, user.getId(), Integer.parseInt(params.get("status").toString()));
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("getTodayOrderPageWithCover")
    @ApiOperation("今日订单(志愿者)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = "status", value = "订单状态： 0未接单 1已接单 2已完成 3已取消", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<PensionOrderDTO>> getTodayOrderPageByStatus(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getTodayOrderPage(params);
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("getElderTodayOrderPage")
    @ApiOperation("今日订单(老人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<PensionOrderDTO>> getElderTodayOrderPage(@ApiIgnore @LoginUser UserEntity creator,
                                                                    @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getElderTodayOrderPage(params, creator.getId());
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("orderInfo/{id}")
    @ApiOperation("预约订单详情(志愿者)")
    public Result<PensionOrderDTO> getOrderInfo(@ApiIgnore @LoginUser UserEntity user,
                                                @PathVariable("id") Long id){
        PensionOrderDTO data = pensionOrderService.getOrderInfoByIdAndUserIdOrStatus(id, null, null);
        return new Result<PensionOrderDTO>().ok(data);
    }

    @Login
    @PutMapping("takingOrder/{id}")
    @ApiOperation("接单(志愿者)")
    public Result takingOrder(@ApiIgnore @LoginUser UserEntity user,
                              @PathVariable("id") Long id){
        PensionOrderDTO pensionOrderDTO = pensionOrderService.getOrderInfoByIdAndStatus(id, Constant.OrderStatus.BOOKED.geStatus());
        Date recipientTime = new Date();
        pensionOrderDTO.setOrderRecipientId(user.getId());
        pensionOrderDTO.setOrderRecipientName(user.getUsername());
        pensionOrderDTO.setOrderRecipientPhone(user.getMobile());
        pensionOrderDTO.setOrderRecipientTime(recipientTime);
        pensionOrderDTO.setUpdateDate(recipientTime);
        pensionOrderDTO.setStatus(Constant.OrderStatus.ORDER.geStatus());
        pensionOrderDTO.setStatusName("已接单");
        pensionOrderService.update(pensionOrderDTO);

        return new Result();
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<PensionOrderDTO> get(@PathVariable("id") Long id){
        PensionOrderDTO data = pensionOrderService.get(id);

        return new Result<PensionOrderDTO>().ok(data);
    }

    @Login
    @PostMapping("reservationOrder")
    @ApiOperation("预约保存")
    public Result reservationOrder(@ApiIgnore @LoginUser UserEntity user,@RequestBody ReservationDTO dto){

        PensionOrderDTO data = new PensionOrderDTO().assemblePensionOrder(dto,user);
        pensionOrderService.reservation(data);
        return new Result();
    }

    @PostMapping
    @ApiOperation("保存")
    public Result save(@RequestBody PensionOrderDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        pensionOrderService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    public Result update(@RequestBody PensionOrderDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        pensionOrderService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //校验数据
        AssertUtils.isArrayEmpty(ids, "id");

        pensionOrderService.delete(ids);

        return new Result();
    }

}
