package com.younike.admin;

import com.alibaba.fastjson.JSONObject;
import com.younike.common.annotation.Log;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.core.page.TableDataInfo;
import com.younike.common.enums.BusinessType;
import com.younike.common.utils.poi.ExcelUtil;
import com.younike.system.domain.Merchant;
import com.younike.system.domain.vo.MerchantSettledVO;
import com.younike.system.param.MerchantQueryParam;
import com.younike.system.service.IMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 店铺信息Controller
 *
 * @author elev
 * @date 2022-01-06
 */
@Api("后台店铺信息接口")
@RestController
@RequestMapping("/admin/merchant")
public class AdminMerchantController extends BaseController {
    @Autowired
    private IMerchantService merchantService;

    /**
     * 查询店铺信息列表
     */
    @ApiOperation("查询店铺信息列表")
    @PreAuthorize("@ss.hasPermi('system:merchant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Merchant merchant) {
        startPage();
        System.err.println(JSONObject.toJSONString(merchant));
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 查询店铺信息列表
     */
    @ApiOperation("查询店铺信息列表")
    @PreAuthorize("@ss.hasPermi('system:merchant:list')")
    @GetMapping("/auditList")
    public TableDataInfo auditList(MerchantQueryParam param) {
        startPage();
        List<MerchantSettledVO> list = merchantService.selectAuditMerchantList(param);
        return getDataTable(list);
    }

    /**
     * 查询店铺信息列表
     */
    @ApiOperation("查询审核通过店铺信息列表")
    @PreAuthorize("@ss.hasPermi('system:merchant:list')")
    @GetMapping("/passList")
    public TableDataInfo passList(MerchantQueryParam param) {
        startPage();
        List<MerchantSettledVO> list = merchantService.selectPassMerchantList(param);
        return getDataTable(list);
    }
    /**
     * 查询店铺信息列表
     */
    @ApiOperation("查询店铺信息详情")
    @PreAuthorize("@ss.hasPermi('system:merchant:list')")
    @GetMapping("/merchantInfo")
    public AjaxResult merchantInfo(@RequestParam("merchantId") Long merchantId) {
        return AjaxResult.success(merchantService.merchantInfo(merchantId));
    }

    @PreAuthorize("@ss.hasPermi('system:merchant:edit')")
    @ApiOperation("审核处理")
    @PutMapping("/handleAudit")
    public AjaxResult handleAudit(@RequestBody Merchant merchant) {
        if (merchant == null || merchant.getMerchantId() == null || StringUtils.isEmpty(merchant.getShopState())) {
            return AjaxResult.error("参数错误");
        }
        //todo 获取当前修改用户信息
        merchant.setExamineUser(null);
        merchant.setExamineTime(LocalDateTime.now());
        return toAjax(merchantService.updateMerchant(merchant));
    }

    @PreAuthorize("@ss.hasPermi('system:merchant:edit')")
    @ApiOperation("下架店铺")
    @PutMapping("/takeOff")
    public AjaxResult takeOff(@RequestBody Merchant merchant) {
        if (merchant == null || merchant.getMerchantId() == null || StringUtils.isEmpty(merchant.getCloseReason())) {
            return AjaxResult.error("参数错误");
        }
        //todo 获取当前修改用户信息
        merchant.setCloseUser(null);
        merchant.setCloseTime(LocalDateTime.now());
        merchant.setBusinessState("002");
        return toAjax(merchantService.updateMerchant(merchant));
    }

    @PreAuthorize("@ss.hasPermi('system:merchant:edit')")
    @ApiOperation("上架店铺")
    @PutMapping("/takeOn")
    public AjaxResult takeOn(@RequestBody Merchant merchant) {
        if (merchant == null || merchant.getMerchantId() == null) {
            return AjaxResult.error("参数错误");
        }
        //todo 获取当前修改用户信息
        merchant.setBusinessState("001");
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 导出店铺信息列表
     */
    @ApiOperation("导出店铺信息列表")
    @PreAuthorize("@ss.hasPermi('system:merchant:export')")
    @Log(title = "店铺信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Merchant merchant) {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        return util.exportExcel(list, "店铺信息数据");
    }

    /**
     * 获取店铺信息详细信息
     */
    @ApiOperation("获取店铺信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:merchant:query')")
    @GetMapping(value = "/{merchantId}")
    public AjaxResult getInfo(@PathVariable("merchantId") Long merchantId) {
        return AjaxResult.success(merchantService.selectMerchantByMerchantId(merchantId));
    }

    /**
     * 修改店铺信息
     */
    @ApiOperation("修改店铺信息")
    @PreAuthorize("@ss.hasPermi('system:merchant:edit')")
    @Log(title = "店铺信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant) {
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除店铺信息
     */
    @ApiOperation("删除店铺信息")
    @PreAuthorize("@ss.hasPermi('system:merchant:remove')")
    @Log(title = "店铺信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{merchantIds}")
    public AjaxResult remove(@PathVariable Long[] merchantIds) {
        return toAjax(merchantService.deleteMerchantByMerchantIds(merchantIds));
    }
}
