package cn.wisenergy.web.admin.controller.app;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.common.utils.exception.Result;
import cn.wisenergy.model.app.WorkHoliday;
import cn.wisenergy.model.app.WorkSubmitAdopt;
import cn.wisenergy.model.dto.AllRoleAndUserRoleDto;
import cn.wisenergy.model.dto.HolidayAndAutoDto;
import cn.wisenergy.service.WorkHolidayService;
import cn.wisenergy.service.WorkSubmitAdoptService;
import cn.wisenergy.service.WorkUserService;
import cn.wisenergy.web.admin.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author 玛卡巴卡
 */
@RestController
@RequestMapping(value = "/systemManager")
@Slf4j
@Api(tags = "系统管理")
public class SystemController extends BaseController {

    @Autowired
    private WorkHolidayService workHolidayService;

    @Autowired
    private WorkSubmitAdoptService workSubmitAdoptService;

    @Autowired
    private WorkUserService workUserService;

    @ApiOperation(value = "添加工作日历", notes = "添加工作日历", httpMethod = "POST")
    @ApiImplicitParam(name = "workHoliday", value = "节假日实体类",dataType = "WorkHoliday",required = true)
    @PostMapping("/addHoliday")
    public Result<Boolean> addHoliday(@RequestBody WorkHoliday workHoliday) {
        log.info("SystemController[]addHoliday[]input.param.workHoliday:{}" + workHoliday);
        Boolean aBoolean = workHolidayService.addHoliday(workHoliday);
        return getResult(aBoolean);
    }
    @ApiOperation(value = "编辑工作日历", notes = "编辑工作日历", httpMethod = "PUT")
    @ApiImplicitParam(name = "workHoliday", value = "节假日实体类",dataType = "WorkHoliday",required = true)
    @PutMapping("/modifyHoliday")
    public Result<Boolean> modifyHoliday(@RequestBody WorkHoliday workHoliday) {
        log.info("SystemController[]modifyHoliday[]input.param.workHoliday:{}" + workHoliday);
        Boolean aBoolean = workHolidayService.modifyHoliday(workHoliday);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "删除工作日历", notes = "删除工作日历", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "节假日id",dataType = "int",required = true)
    @DeleteMapping("/deleteHoliday")
    public Result<Boolean> deleteHoliday(Integer id) {
        log.info("SystemController[]deleteHoliday[]input.param.id:{}" + id);
        Boolean aBoolean = workHolidayService.deleteHoliday(id);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "获取工作日历和审批时限", notes = "获取工作日历和审批时限", httpMethod = "GET")
    @ApiImplicitParam(name = "userId", value = "管理员id", dataType = "int", required = true)
    @GetMapping("/getHolidayAndAuto")
    public Result<HolidayAndAutoDto> getHolidayAndAuto(Integer userId) {
        log.info("SystemController[]getHolidayAndAuto[]input.param.userId:{}" + userId);
        HolidayAndAutoDto holidayAll = workHolidayService.getHolidayAll(userId);
        return getResult(holidayAll);
    }

    @ApiOperation(value = "修改审批时限", notes = "修改审批时限", httpMethod = "PUT")
    @ApiImplicitParam(name = "workSubmitAdopt",value = "审批时限",dataType ="WorkSubmitAdopt", required = true)
    @PutMapping("/modifyAuto")
    public Result<Boolean> modifyAuto(@RequestBody WorkSubmitAdopt workSubmitAdopt) {
        log.info("SystemController[]modifyAuto[]input.param.workSubmitAdopt:{}" + workSubmitAdopt);
        Boolean aBoolean = workSubmitAdoptService.modifyAuto(workSubmitAdopt);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "获取账号列表", notes = "获取账号列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "管理员id",dataType = "int",required = true),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "记录数",dataType = "int",required = true)
    })
    @GetMapping("/getAllRole")
    public Result<AllRoleAndUserRoleDto> getAllRole(Integer userId,Integer page,Integer pageSize) {
        log.info("SystemController[]getAllRole[]input.param.userId:{},page:{},pageSize:{}" + userId, page, pageSize);
        AllRoleAndUserRoleDto allRoleAndUserRoleDto = workUserService.getUserRoleDto(userId,page,pageSize);
        return getResult(allRoleAndUserRoleDto);
    }

    @ApiOperation(value = "设置角色", notes = "设置角色", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",dataType = "int",required = true),
            @ApiImplicitParam(name = "roleIds", value = "角色ids", dataType = "String", required = true)
    })
    @PostMapping("/modifyRole")
    public Result<Boolean> modifyRole(Integer userId,String roleIds) {
        log.info("SystemController[]modifyRole[]input.param.userId:{},roleIds:{}" + userId, roleIds);
        if (null == userId || StringUtils.isBlank(roleIds)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        String[] split = roleIds.split("\\,");
        Integer[] newIds = (Integer[]) ConvertUtils.convert(split, Integer.class);
        List<Integer> lists = new ArrayList<>(Arrays.asList(newIds));
        Boolean aBoolean = workUserService.modifyRole(userId, lists);
        return getResult(aBoolean);
    }

}
