package io.hmit.modules.serviceOrder.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.hmit.annotation.Login;
import io.hmit.common.constant.Constant;
import io.hmit.common.page.PageData;
import io.hmit.common.utils.Result;
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.serviceOrder.dto.PensionOrderEvaluationDTO;
import io.hmit.modules.serviceOrder.service.PensionOrderEvaluationService;
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.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import java.net.URLDecoder;
import java.util.Map;


/**
 * 订单评价表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-03-11
 */
@RestController
@RequestMapping("serviceOrder/pensionorderevaluation")
@Api(tags="订单评价表")
public class PensionOrderEvaluationController {
    @Autowired
    private PensionOrderEvaluationService pensionOrderEvaluationService;

    @Autowired
    private PensionOrderService pensionOrderService;

    @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<PensionOrderEvaluationDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<PensionOrderEvaluationDTO> page = pensionOrderEvaluationService.page(params);

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

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

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

    @Login
    @PostMapping
    @ApiOperation("保存")
    public Result save(@RequestBody String dto){
        String todto = null;
        try {
            todto = URLDecoder.decode(dto,"UTF-8");
        }catch (Exception ignored){}
        PensionOrderEvaluationDTO pensionOrderEvaluationDTO = JSON.parseObject(todto, PensionOrderEvaluationDTO.class);
        pensionOrderEvaluationDTO.setFile(StringUtils.join(pensionOrderEvaluationDTO.getFiles(), ","));
        pensionOrderEvaluationService.save(pensionOrderEvaluationDTO);

        pensionOrderService.evaluationDone(pensionOrderEvaluationDTO);
        return new Result();
    }

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

        pensionOrderEvaluationService.update(dto);

        return new Result();
    }

}
