package com.zcxk.controller;

import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.ResultStatus;
import com.zcxk.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 java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.zcxk.entity.ParmSettingEntity;
import com.zcxk.service.ParmSettingService;

/**
 * 参数设置页面控制器
 * @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<ParmSettingEntity>> list(@RequestParam(required = false) Long companyOrgId) {
        ParmSettingEntity parmSettingEntity = new ParmSettingEntity();
        parmSettingEntity.setCompanyOrgId(companyOrgId);
        List<ParmSettingEntity> list = parmSettingService.findList(parmSettingEntity);
        return new AjaxMessage<>(ResultStatus.OK, list);
    }

    @RequestMapping(value = "/findParm", method = RequestMethod.GET)
    @ResponseBody
    public AjaxMessage<ParmSettingEntity> findParm(@RequestParam(required = true) 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 new AjaxMessage<>(ResultStatus.OK, list.get(0));
        } else {
            return new AjaxMessage<>(ResultStatus.OBJECT_ERROR);
        }
    }


    /**
     * 新增
     */
    @ApiOperation(value = "新增参数信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxMessage<Integer> 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 new AjaxMessage<>(ResultStatus.OK, 1);
    }

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

    /**
     * 修改保存考评等级
     */
    @ApiOperation(value = "修改保存参数")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public  AjaxMessage<Integer> 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 new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改保存考评等级
     */
    @ApiOperation(value = "批量保存或更新参数")
    @RequestMapping(value = "/batchSave", method = RequestMethod.POST)
    public  AjaxMessage<Integer> 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 new AjaxMessage<>(ResultStatus.OK, 1);
    }
    /**
     * 删除
     */
    @ApiOperation(value = "批量删除参数")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer> deleteByIds(@ApiParam(value = "参数IDS", required = true) @RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        int result = parmSettingService.delParmSettingByIds(ids) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 删除
     */
    @ApiOperation(value = "单个删除参数")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public AjaxMessage<Integer> deleteById(@ApiParam(value = "参数ID", required = true) @RequestParam Long id) {
        if (id == null) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        List<Long> longIds = new ArrayList<>();
        longIds.add(id);
        ParmSettingEntity parmSetting = parmSettingService.findParmSettingById(id);
        if (parmSetting == null) {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_PARENT_ERROR, 0);
        } else {
            int result = parmSettingService.delParmSettingById(id) ? 1 : 0;
            return new AjaxMessage<>(ResultStatus.OK, result);
        }
    }
}
