package com.softwarebr.nunu.web.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.brframework.common.utils.ConvertObjectUtil;
import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.annatotion.layout.EditAlertLayout;
import com.brframework.commoncms.annatotion.layout.EditLayout;
import com.brframework.commoncms.annatotion.layout.PageLayout;
import com.brframework.commoncms.annatotion.option.CmsOption;
import com.brframework.commoncms.core.Icons;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonweb.json.Page;
import com.brframework.commonweb.json.PageParam;
import com.brframework.commonwebadmin.aop.annotation.AOLog;
import com.softwarebr.nunu.domain.InvestInfo;
import com.softwarebr.nunu.domain.VerifyPageTotal;
import com.softwarebr.nunu.dto.org.InvestOrgParamDTO;
import com.softwarebr.nunu.entity.hall.InvestOrg;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.json.admin.hall.*;
import com.softwarebr.nunu.service.hall.InvestOrgService;
import com.softwarebr.nunu.service.user.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;
import static com.brframework.commondb.core.ControllerPageHandle.jpaPageConvertToPage;

/**
 * @author lilin
 * @date 2020-02-25 12:03:51
 */

@RestController
@Api(tags = "投资机构管理")
@AdminMenu(menuName = "投资机构管理", icon = Icons.TABLE)
@RequestMapping("/admin/access")
public class AdminOrgController {

    @Autowired
    private InvestOrgService investOrgService;

    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "投资机构列表", notes = "投资机构列表", produces = "text/plain")
    @GetMapping("/v1/invest/org/page")
    @AdminMenu(menuName = "投资机构管理", order = 1, url = "investment")
    @PreAuthorize("hasRole('hall-invest-org-query')")
    @PageLayout(
            options = {
                    @CmsOption(name = "添加", uriMappingMethod = "investOrgCreateV1")
            },
            columnOptions = {
                    @CmsOption(name = "查看", uriMappingMethod = "getInvestOrg"),
                    @CmsOption(name = "上架", uriMappingMethod = "investOrgUpperV1"),
                    @CmsOption(name = "下架", uriMappingMethod = "investOrgLowerV1"),
                    @CmsOption(name = "审核", uriMappingMethod = "investOrgVerifyV1"),
                    @CmsOption(name = "修改", uriMappingMethod = "investOrgEditV1")
            }
    )
    public JSONResult<Page<InvestOrgResult>> investOrgListV1(PageParam pageParam, InvestOrgQueryParam param) {
        if (null != param.getAccount()) {
            param.setMemberId(memberService.findMemberIdByPhone(param.getAccount()));
        }
        Page<InvestOrgResult> page = jpaPageConvertToPage(investOrgService.page(pageParam, param), pageParam, InvestOrgResult.class);
        for (InvestOrgResult investOrgResult : page.getList()) {
            if (investOrgResult.getMemberId() > 0) {
                investOrgResult.setAccount(memberService.accessObject(investOrgResult.getMemberId()).getPhone());
            } else {
                investOrgResult.setAccount("平台");
            }
        }
        return JSONResult.ok(page);
    }

    @ApiOperation(value = "投资机构状态数据统计", notes = "投资机构状态数据统计", produces = "text/plain")
    @PreAuthorize("hasRole('hall-invest-org-query')")
    @GetMapping("/v1/invest/org/count")
    public JSONResult<VerifyPageTotal> getInvestOrgCount(InvestOrgQueryParam param) {
        VerifyPageTotal verifyPageResult = new VerifyPageTotal();
        if (null != param.getAccount()) {
            param.setMemberId(memberService.findMemberIdByPhone(param.getAccount()));
        }
        verifyPageResult.setTotalCount(investOrgService.count(param));
        param.setStatus(SystemConst.STATUS_LOWER_SHELF);
        verifyPageResult.setLowerCount(investOrgService.count(param));
        param.setStatus(SystemConst.STATUS_UPPER_SHELF);
        verifyPageResult.setUpperCount(investOrgService.count(param));
        param.setStatus(null);
        param.setVerify(SystemConst.VERIFY_AWAIT);
        verifyPageResult.setVerifyAwaitCount(investOrgService.count(param));
        param.setVerify(SystemConst.VERIFY_ADOPT);
        verifyPageResult.setVerifyAdoptCount(investOrgService.count(param));
        param.setVerify(SystemConst.VERIFY_REJECT);
        verifyPageResult.setVerifyRejectCount(investOrgService.count(param));
        return JSONResult.ok(verifyPageResult);
    }

    @ApiOperation(value = "修改投资机构信息", notes = "修改投资机构信息", produces = "text/plain")
    @PostMapping("/v1/invest/org/{id}")
    @PreAuthorize("hasRole('hall-invest-org-edit-v1')")
    @AOLog
    @EditAlertLayout(
            paddingMappingMethod = "getInvestOrg"
    )
    public JSONResult investOrgEditV1(@PathVariable Long id, @Valid @RequestBody InvestOrgParam param) {
        investOrgService.updateInvestOrg(id, convertTo(param, InvestOrgParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "添加投资机构信息", notes = "添加投资机构信息", produces = "text/plain")
    @PutMapping("/v1/invest/org/add")
    @PreAuthorize("hasRole('hall-invest-org-create-v1')")
    @AOLog
    @EditLayout
    public JSONResult investOrgCreateV1(@Valid @RequestBody InvestOrgParam param) {
        investOrgService.saveInvestOrg(-1L, convertTo(param, InvestOrgParamDTO.class));
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取投资机构信息", notes = "获取投资机构信息", produces = "text/plain")
    @PreAuthorize("hasRole('hall-invest-org-query')")
    @GetMapping("/v1/invest/org/{id}")
    public JSONResult<InvestOrgDetailsResult> getInvestOrg(@PathVariable Long id) {
        InvestOrg investOrg = investOrgService.accessObject(id);
        InvestOrgDetailsResult investOrgResult = convertTo(investOrg, InvestOrgDetailsResult.class);
        investOrgResult.setInvestInfo(JSON.parseArray(investOrg.getInvestInfos(), InvestInfo.class));
        return JSONResult.ok(investOrgResult);
    }

    @ApiOperation(value = "投资机构上架", notes = "投资机构上架", produces = "text/plain")
    @PreAuthorize("hasRole('hall-invest-org-upper-edit-v1')")
    @PostMapping("/v1/invest/org/upper/{id}")
    @AOLog
    public JSONResult investOrgUpperV1(@PathVariable Long id) {
        investOrgService.investOrgUpperShelf(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "投资机构下架", notes = "投资机构下架", produces = "text/plain")
    @PreAuthorize("hasRole('hall-invest-org-lower-edit-v1')")
    @PostMapping("/v1/invest/org/lower/{id}")
    @AOLog
    public JSONResult investOrgLowerV1(@PathVariable Long id) {
        investOrgService.investOrgLowerShelf(id);
        return JSONResult.ok();
    }

    @ApiOperation(value = "投资机构审核", notes = "投资机构审核", produces = "text/plain")
    @PreAuthorize("hasRole('hall-invest-org-verify-edit-v1')")
    @PostMapping("/v1/invest/org/verify/{id}")
    @AOLog
    public JSONResult investOrgVerifyV1(@PathVariable Long id, @Valid @RequestBody InvestVerifyParam param) {
        investOrgService.investOrgVerify(id, param.getVerify(), param.getVerifyRemark());
        return JSONResult.ok();
    }


}
