package cn.iocoder.yudao.module.property.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.property.entity.Property;
import cn.iocoder.yudao.module.property.service.PropertyService;
import cn.iocoder.yudao.module.property.vo.PropertyReqVO;
import cn.iocoder.yudao.module.property.vo.PropertyRespVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 企业管理-资产管理 控制层
 *
 * @author 小李
 * @date 16:41 2024/8/15
 **/
@RestController
@Tag(name = "企业管理 - 资产管理")
@RequestMapping("/company/property")
@Validated
public class PropertyController {

    @Resource
    private PropertyService propertyService;

    /**
     * 企业管理-资产分页
     *
     * @param pageReqVO 查询对象
     * @param pageNo    页码
     * @param pageSize  条数
     * @author 小李
     * @date 16:15 2024/8/15
     **/
    @GetMapping("/page")
    @Operation(summary = "获得企业管理-资产分页")
    @PreAuthorize("@ss.hasPermission('company:property:query')")
    public CommonResult<IPage<?>> getPropertyPage(PropertyReqVO pageReqVO,
                                                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<Property> page = new Page<>(pageNo, pageSize);
        IPage<Property> pageResult = propertyService.getPropertyPage(pageReqVO, page);
        return success(pageResult);
    }


    /**
     * 创建资产
     *
     * @param createReqVO
     * @author 小李
     * @date 18:26 2024/8/15
     **/
    @PostMapping("/create")
    @Operation(summary = "创建企业管理-资产")
    @PreAuthorize("@ss.hasPermission('company:property:create')")
    public CommonResult createProperty(@RequestBody PropertyReqVO createReqVO) {
        propertyService.createProperty(createReqVO);
        return CommonResult.ok();
    }

    /**
     * 获得资产记录
     *
     * @param id 资产ID
     * @author 小李
     * @date 18:45 2024/8/15
     **/
    @GetMapping("/get")
    @Operation(summary = "获得企业管理-资产")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('company:property:query')")
    public CommonResult<PropertyRespVO> getProperty(@RequestParam("id") String id) {
        Property property = propertyService.getProperty(id);
        return success(BeanUtils.toBean(property, PropertyRespVO.class));
    }

    /**
     * 更新资产
     * @author 小李
     * @date 18:57 2024/8/15
     * @param updateReqVO 资产对象
    **/
    @PutMapping("/update")
    @Operation(summary = "更新企业管理-资产")
    @PreAuthorize("@ss.hasPermission('company:property:update')")
    public CommonResult<Boolean> updateProperty(@RequestBody PropertyReqVO updateReqVO) {
        propertyService.updateProperty(updateReqVO);
        return success(true);
    }

    /**
     * 删除资产
     * @author 小李
     * @date 19:20 2024/8/15
     * @param id 资产ID
    **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除企业管理-资产")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('company:property:delete')")
    public CommonResult<Boolean> deleteProperty(@RequestParam("id") String id) {
        propertyService.deleteProperty(id);
        return success(true);
    }

    /**
     * 导出资产数据
     * @author 小李
     * @date 20:02 2024/8/15
     * @param pageReqVO
     * @param response
    **/
    @GetMapping("/export-excel")
    @Operation(summary = "导出企业管理-资产 Excel")
    @PreAuthorize("@ss.hasPermission('company:property:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPropertyExcel(PropertyReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        int pageNo = 1, pageSize = 100;
        List<Property> list = new ArrayList<>();
        while (true){
            Page<Property> page = new Page<>(pageNo, pageSize);
            IPage<Property> propertyPage = propertyService.getPropertyPage(pageReqVO, page);
            if (ObjectUtil.isNotEmpty(propertyPage) && !propertyPage.getRecords().isEmpty()){
                list.addAll(propertyPage.getRecords());
                pageNo++;
            }else {
                break;
            }
        }
        // 导出 Excel
        Map<Integer, Integer> columnWidthMap = new HashMap<>();
        // 第一列的索引是0，宽度设置为20个字符宽
        columnWidthMap.put(9, 20);
        columnWidthMap.put(10, 20);
        ExcelUtils.write(response, "企业管理-资产.xls", "数据", Property.class, list, columnWidthMap);
    }

}