package com.ozs.web.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ozs.common.core.domain.R;
import com.ozs.common.core.domain.entity.SysProcurementStandard;
import com.ozs.common.core.domain.entity.SysRole;
import com.ozs.plan.doman.MonthlyReconciliation;
import com.ozs.system.domain.vo.requestVo.SysProcurementStandardVo;
import com.ozs.system.service.SysProcurementStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 采购标准前端控制器
 * </p>
 *
 * @author ytrd
 * @since 2022-11-02
 */
@Api(tags = "采购标准接口")
@RestController
@RequestMapping("system/SysProcurementStandard")
@CrossOrigin
public class SysProcurementStandardController {

    @Resource
    private SysProcurementStandardService sysProcurementStandardService;

    @ApiOperation("添加采购单位管理中标准信息")
    @PostMapping("save")
    public R<String> save(@RequestBody List<SysProcurementStandardVo> sysProcurementStandardVoList) {
        boolean isSuccess = false;
        SysProcurementStandard sysProcurementStandard = new SysProcurementStandard();
        for (SysProcurementStandardVo sysProcurementStandardVo : sysProcurementStandardVoList) {
            BeanUtils.copyProperties(sysProcurementStandardVo, sysProcurementStandard);
            isSuccess = sysProcurementStandardService.save(sysProcurementStandard);
            if (!isSuccess) {
                return R.fail();
            }
        }
        return R.ok();
    }

    @ApiOperation("修改采购单位管理中标准信息")
    @PostMapping("update")
    public R<String> update(@RequestBody List<SysProcurementStandardVo> sysProcurementStandardVoList) {
        boolean isSuccess = false;
        SysProcurementStandard sysProcurementStandard = new SysProcurementStandard();
        LambdaQueryWrapper<SysProcurementStandard> lw = new LambdaQueryWrapper<>();
        for (SysProcurementStandardVo sysProcurementStandardVo : sysProcurementStandardVoList) {
            BeanUtils.copyProperties(sysProcurementStandardVo, sysProcurementStandard);
            if(sysProcurementStandard.getProcurementStandardId()>-1L){
                lw.eq(SysProcurementStandard::getProcurementStandardId,sysProcurementStandard.getProcurementStandardId());
            }
            isSuccess = sysProcurementStandardService.update(sysProcurementStandard,lw);
            if (!isSuccess) {
                return R.fail();
            }
        }
        return R.ok();
    }
}

