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.LpbHouse;
import com.cool.bang.modules.business.service.ILpbHouseService;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * 房屋信息
 *
 * @author Sy
 * @version 1.0.0
 * 2024-10-22 09:20
 */
@RestController
@RequestMapping(ControllerConstant.BUSINESS + "house/v1")
public class LpbHouseController extends BaseController {
    @Autowired
    private ILpbHouseService lpbHouseService;

    @Autowired
    private IResourceService resourceService;


    @GetMapping
    @PreAuthorize("hasPermission('','business:house:main')")
    public ResultTable main(PageDomain pageDomain, LpbHouse param) {
        PageInfo<LpbHouse> lpbHousePage = lpbHouseService.page(pageDomain, param);
        return pageTable(lpbHousePage.getList(), lpbHousePage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','business:house:main')")
    public Result get(@PathVariable Long id) {
        LpbHouse lpbHouse =  lpbHouseService.queryById(id);
        lpbHouse.setResources(resourceService.queryByTableId(id,2));
        return success(lpbHouse);
    }

    /**
     * 通过单元主键查询单元下所有房屋
     *
     * @param unitId 单元主键
     * @return RestResponse
     */
    @GetMapping("/houseList")
    public Result getList(Integer unitId) {
        List<LpbHouse> list = lpbHouseService.queryByUnit(unitId);
        return success(list);
    }

    /**
     * 通过娄主键查询单元下所有房屋
     *
     * @param buildingId 楼主键
     * @return RestResponse
     */
    @GetMapping("/houseBuilList")
    public Result gethouseBuilList(Integer buildingId) {
        List<LpbHouse> list = lpbHouseService.queryByBuild(buildingId);
        return success(list);
    }

    @PostMapping
    public Result add(LpbHouse lpbHouse) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String code = dtf.format(LocalDateTime.now());
        lpbHouse.setHouseNumber(code);
        lpbHouse.insertConfigure();
        resourceService.saveImg(lpbHouse.getResources(),lpbHouse.getId());
        return decide(lpbHouseService.save(lpbHouse));
    }

    @PostMapping("edit")
    public Result edit(LpbHouse lpbHouse) {
        resourceService.deleteTableId(lpbHouse.getId(),2);
        resourceService.saveImg(lpbHouse.getResources(),lpbHouse.getId());
        return decide(lpbHouseService.update(lpbHouse));
    }

    @GetMapping("remove/{id}")
    public Result remove(@PathVariable Integer id) {
        return decide(lpbHouseService.removeById(id));
    }

}
