package com.qxgmat.controller.admin;

import com.github.pagehelper.Page;
import com.nuliji.tools.PageMessage;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.dao.entity.Ad;
import com.qxgmat.dto.admin.request.AdDto;
import com.qxgmat.dto.admin.response.AdListDto;
import com.qxgmat.service.inline.AdService;
import com.qxgmat.service.inline.ManagerLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController("AdminAdController")
@RequestMapping("/admin/ad")
@Api(tags = "广告接口", description = "广告相关", produces = MediaType.APPLICATION_JSON_VALUE)
public class AdController {
    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private AdService adService;
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加广告", httpMethod = "POST")
    public Response<Ad> add(@RequestBody @Validated AdDto dto, HttpServletRequest request) {
        Ad entity = Transform.dtoToEntity(dto);
        entity = adService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改广告", httpMethod = "PUT")
    public Response<Boolean> edit(@RequestBody @Validated AdDto dto, HttpServletRequest request) {
        Ad entity = Transform.dtoToEntity(dto);
        entity = adService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除广告", httpMethod = "DELETE")
    public Response<Boolean> delete(@RequestParam int id, HttpServletRequest request) {
        managerLogService.log(request);
        return ResponseHelp.success(adService.delete(id));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取广告", httpMethod = "GET")
    public Response<Ad> detail(@RequestParam int id, HttpSession session) {
        Ad entity = adService.get(id);
        return ResponseHelp.success(entity);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "广告列表", httpMethod = "GET")
    public Response<PageMessage<AdListDto>> list(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            HttpSession session) {
        Page<Ad> p = adService.select(page, size);
        List<AdListDto> pr = Transform.convert(p, AdListDto.class);
        return ResponseHelp.success(pr, page, size, p.getTotal());
    }
}
