package io.hmit.modules.appointment.controller;

import io.hmit.common.annotation.LogOperation;
import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData;
import io.hmit.common.utils.ExcelUtils;
import io.hmit.common.utils.Result;
import io.hmit.common.utils.sm.SMUtil;
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.modules.appointment.dto.AppointmentOrderDTO;
import io.hmit.modules.appointment.excel.AppointmentOrderExcel;
import io.hmit.modules.appointment.service.AppointmentOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 预约订单表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-02-13
 */
@RestController
@RequestMapping("appointment/appointmentorder")
@Api(tags="预约订单表")
public class AppointmentOrderController {
    @Autowired
    private AppointmentOrderService appointmentOrderService;

    @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")
    })
    @RequiresPermissions("appointment:appointmentorder:page")
    public Result<PageData<AppointmentOrderDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AppointmentOrderDTO> page = appointmentOrderService.page(params);
        page.getList().forEach(item -> {
            item.setAppointmentPhone(SMUtil.SM4Decrypt(item.getAppointmentPhone()));
            item.setAppointmentIdCard(SMUtil.SM4Decrypt(item.getAppointmentIdCard()));
        });
        return new Result<PageData<AppointmentOrderDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("appointment:appointmentorder:info")
    public Result<AppointmentOrderDTO> get(@PathVariable("id") Long id){
        AppointmentOrderDTO data = appointmentOrderService.get(id);
        data.setAppointmentPhone(SMUtil.SM4Decrypt(data.getAppointmentPhone()));
        data.setAppointmentIdCard(SMUtil.SM4Decrypt(data.getAppointmentIdCard()));
        return new Result<AppointmentOrderDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("appointment:appointmentorder:save")
    public Result save(@RequestBody AppointmentOrderDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setAppointmentPhone(SMUtil.SM4Encrypt(dto.getAppointmentPhone()));
        dto.setAppointmentIdCard(SMUtil.SM4Encrypt(dto.getAppointmentIdCard()));

        appointmentOrderService.save(dto);

        return new Result();
    }



/*
    @GetMapping(value = "/signin/{appointmentOrderId}")
    @ApiOperation("签到")
    @LogOperation("签到")
    public Result update(@PathVariable(value = "appointmentOrderId") Long appointmentOrderId){

        //根据appointmentOrderId获取到订单实体
        AppointmentOrderDTO appointmentOrderDTO = appointmentOrderService.get(appointmentOrderId);
        if (appointmentOrderDTO.getAppointmentTime().getTime() > new Date().getTime()) {
            appointmentOrderDTO.setStatus(2);
            appointmentOrderDTO.setStatusName("签到");
        }

        appointmentOrderService.update(appointmentOrderDTO);

        return new Result();
    }
*/

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

        appointmentOrderService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("appointment:appointmentorder:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AppointmentOrderDTO> list = appointmentOrderService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, AppointmentOrderExcel.class);
    }

}
