package com.fuint.module.backendApi.controller;

import com.fuint.common.dto.AccountInfo;
import com.fuint.common.param.LookWorkPage;
import com.fuint.common.service.LookService;
import com.fuint.common.util.TokenUtil;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 看看作品管理 controller
 *
 * Created by Codex
 */
@Api(tags = "管理端-看看作品相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/backendApi/look")
public class BackendLookController extends BaseController {

    private LookService lookService;

    @ApiOperation(value = "作品列表查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('content:look:index')")
    public ResponseObject list(@ModelAttribute LookWorkPage page) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0) {
            page.setMerchantId(accountInfo.getMerchantId());
        }
        if (accountInfo.getStoreId() != null && accountInfo.getStoreId() > 0) {
            page.setStoreId(accountInfo.getStoreId());
        }

        PaginationResponse<Map<String, Object>> paginationResponse = lookService.queryWorkListForAdmin(page);
        Map<String, Object> result = new HashMap<>();
        result.put("dataList", paginationResponse);
        return getSuccessResult(result);
    }

    @ApiOperation(value = "作品详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('content:look:index')")
    public ResponseObject info(@PathVariable("id") Integer id) throws BusinessCheckException {
        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        Map<String, Object> workInfo = lookService.getWorkDetailForAdmin(id);

        Integer merchantId = workInfo.get("merchantId") == null ? 0 : Integer.parseInt(workInfo.get("merchantId").toString());
        if (accountInfo.getMerchantId() != null && accountInfo.getMerchantId() > 0 && !accountInfo.getMerchantId().equals(merchantId)) {
            return getFailureResult(1004);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("workInfo", workInfo);
        return getSuccessResult(result);
    }

    @ApiOperation(value = "更新作品状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @CrossOrigin
    @PreAuthorize("@pms.hasPermission('content:look:edit')")
    public ResponseObject updateStatus(@RequestBody Map<String, Object> params) throws BusinessCheckException {
        Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
        String status = params.get("status") == null ? "A" : params.get("status").toString();
        if (id <= 0) {
            return getFailureResult(201, "作品ID不能为空");
        }

        AccountInfo accountInfo = TokenUtil.getAccountInfo();
        lookService.updateWorkStatus(id, status, accountInfo.getAccountName(), accountInfo.getMerchantId());
        return getSuccessResult(true);
    }
}
