package com.dgtis.dorm.manager.admin.web;

import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.domain.Banner;
import com.dgtis.dorm.manager.db.service.BannerService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;


    /**
     * @description 描述：获取banner信息列表
     * @author      koucx
     * @return      Object
     * @date        2019/4/24 0024 16:49
     * @param
     */
    @GetMapping("list")
    public Object list(@LoginAdmin Integer adminId, String title,
                       @RequestParam(value = "page", defaultValue = "1")Integer page,
                       @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                       String sort) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        Banner banner = new Banner();
        banner.setTitle(title);
        List<Banner> lstBanner = bannerService.querySelective(banner, page, limit, sort);

        int total = bannerService.countSelective(banner);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", lstBanner);
        data.put("total", total);

        return ResponseUtil.ok(data);
    }


    /**
     * @description 描述：获取指定id的banner详情
     * @author      koucx
     * @return      Object
     * @date        2019/4/24 0024 16:50
     * @param
     */
    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId, Integer id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null) {
            return  ResponseUtil.badArgumentValue();
        }

        Banner banner = bannerService.findById(id);

        return ResponseUtil.ok(banner);
    }


    /**
     * @description 描述：创建banner信息
     * @author      koucx
     * @return      Object
     * @date        2019/4/24 0024 16:51
     * @param
     */
    @PostMapping("create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody Banner banner) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        banner.setCreatorId(adminId);
        banner.setCreateDate(LocalDateTime.now());
        bannerService.add(banner);

        return ResponseUtil.ok(banner);
    }


    /**
     * @description 描述：更新banner信息
     * @author      koucx
     * @return      Object
     * @date        2019/4/24 0024 16:51
     * @param
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody Banner banner) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        bannerService.update(banner);

        return ResponseUtil.ok(banner);
    }


    /**
     * @description 描述：删除banner信息
     * @author      koucx
     * @return      Object
     * @date        2019/4/24 16:52
     * @param
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody Banner banner) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(banner == null) {
            return ResponseUtil.badArgumentValue();
        }

        bannerService.delete(banner.getId());

        return ResponseUtil.ok();
    }
}
