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.RestTree;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.business.domain.LpbCommunity;
import com.cool.bang.modules.business.service.ILpbCommunityService;
import com.cool.bang.modules.system.domain.po.Resource;
import com.cool.bang.modules.system.service.IResourceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 小区门店信息
 *
 * @author Sy
 * @version 1.0.0
 * 2024-10-22 09:08
 */
@RestController
@RequestMapping(ControllerConstant.BUSINESS + "community/v1")
public class LpbCommunityController extends BaseController {

    @Autowired
    private ILpbCommunityService lpbCommunityService;

    @Autowired
    private IResourceService resourceService;


    @GetMapping
    @PreAuthorize("hasPermission('','business:community:main')")
    public ResultTable main(PageDomain pageDomain, LpbCommunity param) {
        PageInfo<LpbCommunity> lpbCommunityPage = lpbCommunityService.page(pageDomain, param);
        return pageTable(lpbCommunityPage.getList(), lpbCommunityPage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','business:community:main')")
    public Result get(@PathVariable Long id) {
        LpbCommunity lpbCommunity = lpbCommunityService.queryById(id);
        List<Resource> resourceList = resourceService.queryByTableId(id,1);
        resourceList.addAll(resourceService.queryByTableId(id,3));
        lpbCommunity.setResources(resourceList);
        return success(lpbCommunity);
    }

    /**
     * 查询楼幢树
     * @param id 小区/项目主键
     * @return
     */
    @GetMapping("/tree")
    public RestTree getTree(String id) {
        RestTree result = new RestTree();
        List<Map<String, Object>> list = lpbCommunityService.selectBuildingTree(Integer.valueOf(id));
        result.setData(list);
        return result;
    }

    @PostMapping
    @PreAuthorize("hasPermission('','business:community:add')")
    public Result add(LpbCommunity lpbCommunity) {
        lpbCommunity.insertConfigure();
        resourceService.saveImg(lpbCommunity.getResources(),lpbCommunity.getId());
        return decide(lpbCommunityService.save(lpbCommunity));
    }

    @PostMapping("edit")
    @PreAuthorize("hasPermission('','business:community:edit')")
    public Result edit(LpbCommunity lpbCommunity) {
        resourceService.deleteTableId(lpbCommunity.getId(),1);
        resourceService.deleteTableId(lpbCommunity.getId(),3);
        resourceService.saveImg(lpbCommunity.getResources(),lpbCommunity.getId());
        return decide(lpbCommunityService.update(lpbCommunity));
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasPermission('','business:community:del')")
    public Result remove(@PathVariable Integer id) {
        return decide(lpbCommunityService.removeById(id));
    }

}
