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.app.WorkType;
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.WorkTypeService;
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;

    @Autowired
    private WorkTypeService workTypeService;


    @ApiOperation(value = "添加和编辑工作日历", notes = "添加和编辑工作日历", httpMethod = "PUT")
    @ApiImplicitParam(name = "workHoliday", value = "节假日实体类",dataType = "WorkHoliday",required = true)
    @PutMapping("/addAndModifyHoliday")
    public Result<Boolean> addAndModifyHoliday(@RequestBody WorkHoliday workHoliday) {
        log.info("SystemController[]modifyHoliday[]input.param.workHoliday:{}" + workHoliday);
        Boolean aBoolean = workHolidayService.addAndModifyHoliday(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.getHoliday(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),
            @ApiImplicitParam(name = "status",value = "账号状态",dataType = "int"),
            @ApiImplicitParam(name = "key",value = "模糊搜索",dataType = "string")
    })
    @GetMapping("/getAllRole")
    public Result<AllRoleAndUserRoleDto> getAllRole(Integer userId,Integer page,Integer pageSize,Integer status,String key) {
        log.info("SystemController[]getAllRole[]input.param.userId:{},page:{},pageSize:{},status:{},key:{}" + userId, page, pageSize,status,key);
        AllRoleAndUserRoleDto allRoleAndUserRoleDto = workUserService.getUserRoleDto(userId,page,pageSize,status,key);
        return getResult(allRoleAndUserRoleDto);
    }

    @ApiOperation(value = "设置角色", notes = "设置角色", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",dataType = "int",required = true),
            @ApiImplicitParam(name = "roleIds", value = "角色ids", dataType = "String", required = true),
            @ApiImplicitParam(name = "status",value = "用户状态",dataType = "int",required = true)
    })
    @PutMapping("/modifyRole")
    public Result<Boolean> modifyRole(Integer userId,String roleIds,Integer status) {
        log.info("SystemController[]modifyRole[]input.param.userId:{},roleIds:{},status:{}" + userId, roleIds,status);
        if (null == userId || StringUtils.isBlank(roleIds) || null == status) {
            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,status);
        return getResult(aBoolean);
    }


    @ApiOperation(value = "添加和修改工时类型", notes = "添加和修改工时类型", httpMethod = "PUT")
    @ApiImplicitParam(name = "workType",value = "工时类型",dataType ="WorkType", required = true)
    @PutMapping("/addAndModifyWorkType")
    public Result<Boolean> addAndModifyWorkType(@RequestBody WorkType workType) {
        log.info("SystemController[]modifyRole[]input.param.workType:{}" + workType);
        Boolean aBoolean = workTypeService.addAndModifyWorkType(workType);
        return getResult(aBoolean);
    }

    @ApiOperation(value = "删除工时类型", notes = "删除工时类型", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id",value = "类型id",dataType ="int", required = true)
    @DeleteMapping("/modifyWorkType")
    public Result<Boolean> deleteTypeById(Integer id) {
        log.info("SystemController[]modifyRole[]input.param.id:{}" + id);
        Boolean aBoolean = workTypeService.deleteTypeById(id);
        return getResult(aBoolean);
    }

}
