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.service.PensionOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
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("todayOrderPage")
    @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>> todayOrderPage(@ApiIgnore @LoginUser UserEntity user,
                                                            @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getTodayOrderPage(params, user.getId());

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

    @Login
    @GetMapping("incompleteOrderPage")
    @ApiOperation(value = "未完成订单分页", notes = "未完成和已接单类似，但此处返回状态为0的订单")
    @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>> incompleteOrderPage(@ApiIgnore @LoginUser UserEntity user,
                                                                 @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getIncompleteOrderPage(params, user.getId());
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("takingOrderPage")
    @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>> takingOrderPage(@ApiIgnore @LoginUser UserEntity user,
                                                             @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getTakingOrderPage(params, user.getId());
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("finishedOrderPage")
    @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>> finishedOrderPage(@ApiIgnore @LoginUser UserEntity user,
                                                               @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getFinishedOrderPage(params, user.getId());
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("canceledOrderPage")
    @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>> canceledOrderPage(@ApiIgnore @LoginUser UserEntity user,
                                                               @ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getCanceledOrderPage(params, user.getId());
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("noProceedOrderPage")
    @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>> noProceedOrderPage(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderDTO> page = pensionOrderService.getNoProceedOrderPage(params);
        return new Result<PageData<PensionOrderDTO>>().ok(page);
    }

    @Login
    @GetMapping("getOrderPageByStatus")
    @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 = "状态", 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("orderInfo/{id}")
    @ApiOperation("预约订单详情")
    public Result<PensionOrderDTO> getOrderInfo(@ApiIgnore @LoginUser UserEntity user,
                                                @PathVariable("id") Long id){
        PensionOrderDTO data = pensionOrderService.getOrderInfoByIdAndUserId(id, user.getId());
        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, 0);
        Date recipientTime = new Date();
        pensionOrderDTO.setOrderRecipientId(user.getId());
        pensionOrderDTO.setOrderRecipientName(user.getUsername());
        pensionOrderDTO.setOrderRecipientPhone(user.getMobile());
        pensionOrderDTO.setOrderRecipientTime(recipientTime);
        pensionOrderDTO.setUpdateDate(recipientTime);
        pensionOrderDTO.setStatus(1); //TODO 后期添加到Constant或新建枚举类
        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);
        return null;
    }

    @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();
    }

}
