package com.cool.bang.modules.business.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.business.domain.Corp;
import com.cool.bang.modules.business.service.ICorpService;
import com.cool.bang.modules.business.service.IDataCollectionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 企业信息
 *
 * @author PeiZhi Guo
 * @version 1.0.0
 * 2024-10-31 17:00
 */
@RestController
@RequestMapping(ControllerConstant.BUSINESS + "corp/v1")
public class CorpController extends BaseController {

    @Autowired
    private ICorpService corpService;

    @Autowired
    private IDataCollectionService dataCollectionService;

    @GetMapping
    @PreAuthorize("hasPermission('','business:corp:main')")
    public ResultTable main(PageDomain pageDomain, Corp param) {
        PageInfo<Corp> clfCorpPage = corpService.page(pageDomain, param);
        return pageTable(clfCorpPage.getList(), clfCorpPage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','business:corp:main')")
    public Result get(@PathVariable Long id) {
        Corp corp = corpService.queryById(id);
        return success(corp);
    }

    @PostMapping("examine")
    @PreAuthorize("hasPermission('','business:corp:examine')")
    public Result examine(@RequestBody @Validated Corp param) {
        param.updateConfigure();
        return decide(corpService.update(param));
    }

    @PostMapping
    @PreAuthorize("hasPermission('','business:corp:add')")
    public Result add(Corp corp) {
        corp.insertConfigure();
        return decide(corpService.save(corp));
    }

    @PostMapping("edit")
    @PreAuthorize("hasPermission('','business:corp:add')")
    public Result edit(Corp corp) {
        return decide(corpService.update(corp));
    }

    @GetMapping("remove/{id}")
    @PreAuthorize("hasPermission('','business:corp:remove')")
    public Result remove(@PathVariable Long id) {
        return decide(corpService.updateOpenid(id));
    }

}
