package com.template.controller;


import com.template.api.SmartDutiesControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.enumModel.eDutiesType;
import com.template.model.enumModel.eIdentityStatu;
import com.template.model.pojo.SmartDuties;
import com.template.model.request.insertDutiesRequest;
import com.template.model.request.updateDutiesRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.DutiesListVo;
import com.template.services.SmartDutiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 应用管理 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-01-16
 */
@RestController
public class SmartDutiesController implements SmartDutiesControllerAPI {

    @Autowired
    private SmartDutiesService smartDutiesService;

    @Override
    public CommonResult insertSmartDuties(insertDutiesRequest idr, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        //去重判断
        SmartDuties existSm = smartDutiesService.querySmartDutiesByName(idr.getName(), idr.getCategoryId());
        if (existSm != null) {
            return CommonResult.fail("系统中存在重复职务，添加失败");
        }

        SmartDuties sm = new SmartDuties();
        sm.setName(idr.getName());
        sm.setCategoryId(idr.getCategoryId());

        int result = smartDutiesService.insertSmartDuties(sm);

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    @Override
    public CommonResult updateSmartDutiesById(updateDutiesRequest udr, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if (udr.getId() == null) {
            return CommonResult.fail("数据ID不能为空");
        }

        SmartDuties oldData = smartDutiesService.getSmartById(udr.getId());
        if (oldData == null) {
            return CommonResult.fail("职务基本信息无效，修改失败");
        }

        //去重判断
        SmartDuties existSm = smartDutiesService.querySmartDutiesByName(udr.getName(), udr.getCategoryId());
        if (existSm != null && !existSm.getId().equals(udr.getId())) {
            return CommonResult.fail("系统中存在重复职务，修改失败");
        }

        oldData.setName(udr.getName() == null ? oldData.getName() : udr.getName());
        oldData.setCategoryId(udr.getCategoryId() == null ? oldData.getCategoryId() : udr.getCategoryId());

        int result = smartDutiesService.updateSmartDuties(oldData);
        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    @Override
    public CommonResult queryPageSmartDutiess(int currentPage, int pageCount, String name) {
        PageUtils<SmartDuties> result = smartDutiesService.queryPageSmartDutiess(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleteSmartDutiesById(int id) {
        SmartDuties data = smartDutiesService.getSmartById(id);

        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = smartDutiesService.deleteSmartDutiesById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    @Override
    public CommonResult queryDutiesCategorys() {
        List<DutiesListVo> result = new ArrayList<>();

        for (int i = 1; i <= 2; i++) {
            DutiesListVo data = new DutiesListVo();
            data.setId(i);
            data.setName(eDutiesType.stringOf(i));
            result.add(data);
        }

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryDutiesByCategory(int categoryId){
        List<DutiesListVo> result = new ArrayList<>();

        List<SmartDuties> datas = smartDutiesService.getSmartDutiesByCategoryId(categoryId);

        for (SmartDuties data:datas) {
            DutiesListVo model = new DutiesListVo();
            model.setId(data.getId());
            model.setName(data.getName());
            result.add(model);
        }

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult queryDutiesByIdentity(int identityId){
        if(identityId == eIdentityStatu.Parent.getValue()){
            return CommonResult.fail("家长身份无法获取职务数据");
        }

        List<DutiesListVo> result = new ArrayList<>();

        int category = identityId == eIdentityStatu.Student.getValue() ? eDutiesType.Student.getValue() : eDutiesType.Teacher.getValue();

        List<SmartDuties> datas = smartDutiesService.getSmartDutiesByCategoryId(category);
        for (SmartDuties data:datas) {
            DutiesListVo model = new DutiesListVo();
            model.setId(data.getId());
            model.setName(data.getName());
            result.add(model);
        }

        return CommonResult.ok(result);
    }
}

