package io.renren.controller;

import java.util.Date;

import io.renren.common.constant.Constant;
import io.renren.common.exception.ErrorCode;
import io.renren.dto.CustomersDTO;
import io.renren.entity.CustomersEntity;
import io.renren.service.CustomersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.dto.CallBackDTO;
import io.renren.service.CallBackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 反馈表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-29
 */
@RestController
@RequestMapping("api/callback")
@Api(tags="反馈表")
public class CallBackController{
    @Autowired
    private CallBackService callBackService;
    @Autowired
    private CustomersService customersService;

    
    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<CallBackDTO> get(@PathVariable("id") Long id){
        CallBackDTO data = callBackService.get(id);
        return new Result<CallBackDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result<String> save(@RequestBody CallBackDTO dto) throws Exception{
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        CustomersDTO customers = customersService.get(dto.getCustomerId());

        if(null == customers || null == customers.getCompanyId()) {
            return new Result<String>().error(ErrorCode.CUSTOMER_NOT_FOUND,"客户id不正确");
        }
        dto.setCompanyId(customers.getCompanyId());
        dto.setCreateDate(new Date());
        dto.setIsPush(Constant.PushStatus.UN_PUSH.getValue());
        dto.setStatus(Constant.Status.ENABLE.getValue());
        callBackService.save(dto);
        return new Result<String>().pageOk(ErrorCode.SUCCESS_CODE,"添加成功");
    }
    
    @PutMapping
    @ApiOperation("修改")
    public Result<CallBackDTO> update(@RequestBody CallBackDTO dto) throws Exception{
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setUpdateTime(new Date());
        callBackService.update(dto);
        return new Result<CallBackDTO>().ok(dto);
    }

}