package com.zcxk.charge.web.controller;


import com.zcxk.charge.core.entity.WaterPropertyEntity;
import com.zcxk.charge.web.service.WaterPropertyService;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用水性质控制层
 * @author yjy
 * @since 2021-5-31
 */
@RestController
@RequestMapping("/waterProperty")
@Api(tags = "用水性质")
public class WaterPropertyController {

    @Autowired
    private WaterPropertyService waterPropertyService;

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "增加用水性质信息")
    public AjaxMessage insert(@ApiParam(value = "用水性质", required = true) @RequestBody WaterPropertyEntity waterPropertyEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        waterPropertyEntity.setTenantId(currentUser.getTenantId());
        waterPropertyEntity.setCreateBy(currentUser.getUsername());
        waterPropertyEntity.setStatus(1);
        return AjaxMessage.success(waterPropertyService.insert(waterPropertyEntity));
    }

    @RequestMapping(value = "delete", method = RequestMethod.GET)
    @ApiOperation(value = "删除用水性质信息")
    public AjaxMessage delete(@ApiParam(value = "用水性质id", required = true) @RequestParam Integer id) {
        return AjaxMessage.success( waterPropertyService.delete(id));
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "修改用水性质信息")
    public AjaxMessage update(@ApiParam(value = "用水性质", required = true) @RequestBody WaterPropertyEntity waterPropertyEntity) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        waterPropertyEntity.setUpdateBy(currentUser.getUsername());
        return AjaxMessage.success( waterPropertyService.update(waterPropertyEntity));
    }

    @RequestMapping(value = "select", method = RequestMethod.GET)
    @ApiOperation(value = "查询用水性质信息")
    public AjaxMessage select(@ApiParam(value = "所属公司id", required = true) @RequestParam Integer companyOrgId) {
        if (companyOrgId==null){
            LoginUser currentUser = UserUtil.getCurrentUser();
            companyOrgId = currentUser.getCompanyId();
        }
        return AjaxMessage.success( waterPropertyService.select(companyOrgId));
    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ApiOperation(value = "批量保存用水性质列表")
    public AjaxMessage insert(@ApiParam(value = "用水性质列表", required = true) @RequestBody List<WaterPropertyEntity> waterPropertyList) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        for (WaterPropertyEntity waterPropertyEntity:waterPropertyList){
            waterPropertyEntity.setTenantId(currentUser.getTenantId());
            waterPropertyEntity.setCreateBy(currentUser.getUsername());
            waterPropertyEntity.setStatus(1);
        }
        return AjaxMessage.success( waterPropertyService.save(waterPropertyList));
    }
}