package com.zcxk.charge.web.controller;

import com.zcxk.charge.api.enums.ChargeErrorEnum;
import com.zcxk.charge.core.entity.ParmSettingEntity;
import com.zcxk.charge.web.service.ParmSettingService;
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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 参数设置页面控制器
 * @author WYY
 * @date 2021-05-11 10:30
 */
@RestController
@RequestMapping("/parmsetting")
@Api(tags = "参数设置")
public class ParmSettingController{

    @Autowired
    private ParmSettingService parmSettingService;

    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage list(@RequestParam(required = false) Long companyOrgId) {
        ParmSettingEntity parmSettingEntity = new ParmSettingEntity();
        parmSettingEntity.setCompanyOrgId(companyOrgId);
        List<ParmSettingEntity> list = parmSettingService.findList(parmSettingEntity);
        return AjaxMessage.success( list);
    }

    @RequestMapping(value = "/findParm", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage findParm(@RequestParam Long companyOrgId,@RequestParam(required = true) Integer parmType) {
        ParmSettingEntity parmSettingEntity = new ParmSettingEntity();
        parmSettingEntity.setCompanyOrgId(companyOrgId);
        parmSettingEntity.setParmType(parmType);
        List<ParmSettingEntity> list = parmSettingService.findList(parmSettingEntity);
        if (list.size() > 0) {
            return AjaxMessage.success(list.get(0));
        } else {
            return AjaxMessage.success(ChargeErrorEnum.OBJECT_ERROR);
        }
    }

    @ApiOperation(value = "新增参数信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxMessage addParm(@ApiParam(value = "参数信息", required = true) @RequestBody ParmSettingEntity parmSetting) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            parmSetting.setUpdateBy(currentUser.getUsername());
            parmSetting.setCreateBy(currentUser.getUsername());
            parmSetting.setDateCreate(new Date());
            parmSetting.setDateUpdate(new Date());
            parmSetting.setTenantId(currentUser.getTenantId());
        }
        parmSetting.setStatus(1);
        parmSettingService.addParmSetting(parmSetting);
        return AjaxMessage.success(1);
    }

    @ApiOperation(value = "按ID查询参数")
    @GetMapping(value = "/findParmById")
    public AjaxMessage findParmById(@ApiParam(value = "参数ID", required = true) @RequestParam  Long id){
        ParmSettingEntity parmSetting = parmSettingService.findParmSettingById(id);
        return AjaxMessage.success( parmSetting);
    }

    @ApiOperation(value = "修改保存参数")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxMessage editParm(@ApiParam(value = "参数信息", required = true) @RequestBody ParmSettingEntity parmSetting){
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            parmSetting.setUpdateBy(currentUser.getUsername());
            parmSetting.setDateUpdate(new Date());
        }
        int result= parmSettingService.updateParmSettingById(parmSetting)? 1: 0;
        return AjaxMessage.success(result);
    }

    @ApiOperation(value = "批量保存或更新参数")
    @RequestMapping(value = "/batchSave", method = RequestMethod.POST)
    public AjaxMessage editEvaluationGrade(@ApiParam(value = "参数信息", required = true) @RequestBody List<ParmSettingEntity> parmSettings) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        for (ParmSettingEntity item : parmSettings) {
            if (currentUser != null) {
                item.setUpdateBy(currentUser.getUsername());
                item.setCreateBy(currentUser.getUsername());
                item.setDateCreate(new Date());
                item.setDateUpdate(new Date());
                item.setTenantId(currentUser.getTenantId());
            }
            item.setStatus(1);
            parmSettingService.saveOrUpdate(item);
        }
        return AjaxMessage.success( 1);
    }

    @ApiOperation(value = "批量删除参数")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage deleteByIds(@ApiParam(value = "参数IDS", required = true) @RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxMessage.success(ChargeErrorEnum.PARAM_ERROR);
        }
        int result = parmSettingService.delParmSettingByIds(ids) ? 1 : 0;
        return AjaxMessage.success(result);
    }

    @ApiOperation(value = "单个删除参数")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public AjaxMessage deleteById(@ApiParam(value = "参数ID", required = true) @RequestParam Long id) {
        if (id == null) {
            return AjaxMessage.success(ChargeErrorEnum.PARAM_ERROR);
        }
        List<Long> longIds = new ArrayList<>();
        longIds.add(id);
        if (parmSettingService.findParmSettingById(id) == null) {
            return AjaxMessage.success(ChargeErrorEnum.EXSIT_IS_PARENT_ERROR, 0);
        }
        int result = parmSettingService.delParmSettingById(id) ? 1 : 0;
        return AjaxMessage.success( result);
    }
}
