package com.hzs.system.sys.controller.manage;


import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.reflect.ReflectUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.sys.SysCorp;
import com.hzs.common.domain.system.sys.ext.SysCorpExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.sys.param.SysCorpParam;
import com.hzs.system.sys.service.ISysCorpService;
import com.hzs.system.sys.vo.SysCorpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组织(公司)设置 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2023-11-20
 */
@RestController
@RequestMapping("/manage/corp")
public class SysCorpController extends BaseController {

    @Autowired
    private ISysCorpService sysCorpService;

    /**
     * 查询列表
     * @return warehouseParam 入参
     */
    @GetMapping("/list")
    @Log(module = EOperationModule.CORP_CONFIG, business = EOperationBusiness.CORP_CONFIG, method = EOperationMethod.SELECT)
    public TableDataInfo list(SysCorpParam sysCorpParam) {
        startPage();
        sysCorpParam.setPkCountry(SecurityUtils.getPkCountry());
        return getTableDataInfo(sysCorpParam);
    }

    /**
     * 查询列表
     * @return warehouseParam 入参
     */
    @GetMapping("/ref")
    public TableDataInfo refCorp(SysCorpParam sysCorpParam) {
        if(sysCorpParam.getPkCountry() == null){
            sysCorpParam.setPkCountry(SecurityUtils.getPkCountry());
        }
        return getTableDataInfo(sysCorpParam);
    }

    private TableDataInfo getTableDataInfo(SysCorpParam sysCorpParam) {
        List<SysCorpExt> sysCorpList = sysCorpService.querySysCorpByCondition(sysCorpParam);
        List<SysCorpVO> sysCorpVOList = new ArrayList<>();
        for (SysCorp sysCorp : sysCorpList) {
            sysCorpVOList.add(BeanUtil.copyProperties(sysCorp, SysCorpVO.class));
        }
        TableDataInfo dataTable = getDataTable(sysCorpList);
        dataTable.setRows(sysCorpVOList);
        return dataTable;
    }

    /**
     * 新增体系配置
     * @param sysCorpParam  入参
     */
    @PostMapping("/save")
    @Log(module = EOperationModule.CORP_CONFIG, business = EOperationBusiness.CORP_CONFIG, method = EOperationMethod.INSERT)
    public AjaxResult save(@RequestBody SysCorpParam sysCorpParam) {sysCorpParam.setPkCountry(SecurityUtils.getPkCountry());
        sysCorpService.saveSysCorp(sysCorpParam, SecurityUtils.getUserId());
        return AjaxResult.success();
    }



    /**
     * 删除
     *
     * @param pkId 主键
     */
    @DeleteMapping("/{pkId}")
    @Log(module = EOperationModule.CORP_CONFIG, business = EOperationBusiness.CORP_CONFIG, method = EOperationMethod.DELETE)
    public AjaxResult delete(@PathVariable Integer pkId) {
        sysCorpService.deleteSysCorpByPk(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }
}
