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


import cn.hutool.core.bean.BeanUtil;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.*;
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.base.ext.BdCubasdocExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.param.BdCubasdocParam;
import com.hzs.system.base.service.IBdCubasdocService;
import com.hzs.system.base.vo.BdCubasdocVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 供应商设置 前端控制器
 */
@RestController
@RequestMapping("/manage/cus")
public class BdCubasdocController extends BaseController {

    @Autowired
    private IBdCubasdocService bdCubasdocService;

    /**
     * 查询列表
     *
     * @return cubasdocParam 入参
     */
    @GetMapping("/list")
    @Log(module = EOperationModule.CUSTOMER_CONFIG, business = EOperationBusiness.CUSTOMER_CONFIG, method = EOperationMethod.SELECT)
    public TableDataInfo list(BdCubasdocParam cubasdocParam) {
        startPage();
        cubasdocParam.setPkCountry(SecurityUtils.getPkCountry());
        List<BdCubasdocExt> bdCubasdocList = bdCubasdocService.queryCubasdocByCondition(cubasdocParam);
        List<BdCubasdocVO> cubasdocVOList = new ArrayList<>();
        bdCubasdocList.forEach(bdCubasdoc -> {
            BdCubasdocVO bdCubasdocVO = BeanUtil.copyProperties(bdCubasdoc, BdCubasdocVO.class);
            cubasdocVOList.add(bdCubasdocVO);
        });
        TableDataInfo dataTable = getDataTable(bdCubasdocList);
        dataTable.setRows(cubasdocVOList);
        return dataTable;
    }

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

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

    /**
     * 查询供应商
     *
     * @return cubasdocParam 入参
     */
    @GetMapping("/list-supplier")
    public TableDataInfo listSupplier(BdCubasdocParam cubasdocParam) {
        cubasdocParam.setPkCountry(SecurityUtils.getPkCountry());
        Integer checkUser = bdCubasdocService.queryThSupplierApplyUserList(SecurityUtils.getUserId());
        if (checkUser > 0) {
            cubasdocParam.setPkCreator(SecurityUtils.getUserId());
        }
        List<BdCubasdocExt> bdCubasdocList = bdCubasdocService.querySupplierByCondition(cubasdocParam);
        List<BdCubasdocVO> cubasdocVOList = new ArrayList<>();
        bdCubasdocList.forEach(bdCubasdoc -> {
            BdCubasdocVO bdCubasdocVO = BeanUtil.copyProperties(bdCubasdoc, BdCubasdocVO.class);
            cubasdocVOList.add(bdCubasdocVO);
        });
        return getDataTable(cubasdocVOList);
    }

    /**
     * 查询供应商(业务领用专用)
     *
     * @return cubasdocParam 入参
     */
    @GetMapping("/list-supplierProduct")
    public TableDataInfo supplierProduct(BdCubasdocParam cubasdocParam) {
        cubasdocParam.setPkCountry(SecurityUtils.getPkCountry());
        Integer checkUser = bdCubasdocService.queryThSupplierApplyUserList(SecurityUtils.getUserId());
        if (checkUser > 0) {
            cubasdocParam.setPkCreator(SecurityUtils.getUserId());
        }
        List<BdCubasdocExt> bdCubasdocList = bdCubasdocService.querySupplierByProduct(cubasdocParam);
        List<BdCubasdocVO> cubasdocVOList = new ArrayList<>();
        bdCubasdocList.forEach(bdCubasdoc -> {
            BdCubasdocVO bdCubasdocVO = BeanUtil.copyProperties(bdCubasdoc, BdCubasdocVO.class);
            cubasdocVOList.add(bdCubasdocVO);
        });
        return getDataTable(cubasdocVOList);
    }

    /**
     * 查询客户
     *
     * @return cubasdocParam 入参
     */
    @GetMapping("/list-customer")
    public TableDataInfo listCustomer(BdCubasdocParam cubasdocParam) {
        cubasdocParam.setPkCountry(SecurityUtils.getPkCountry());
        List<BdCubasdocExt> bdCubasdocList = bdCubasdocService.queryCustomerByCondition(cubasdocParam);
        List<BdCubasdocVO> cubasdocVOList = new ArrayList<>();
        bdCubasdocList.forEach(bdCubasdoc -> {
            BdCubasdocVO bdCubasdocVO = BeanUtil.copyProperties(bdCubasdoc, BdCubasdocVO.class);
            cubasdocVOList.add(bdCubasdocVO);
        });
        return getDataTable(cubasdocVOList);
    }
}
