package com.template.controller;


import com.template.api.SmartSchoolControllerAPI;
import com.template.common.utils.paramUtils;
import com.template.model.pojo.SmartSchool;
import com.template.model.request.UpdateSmartSchoolRequest;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.services.SmartSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@RestController
public class SmartSchoolController implements SmartSchoolControllerAPI {

    @Autowired
    private SmartSchoolService smartSchoolService;

    /**
     * 新增学校基本信息
     * @param smartApply 学校基本信息数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult insertSmartSchool(SmartSchool smartApply, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if(smartApply.getOriginalId() == null){
            smartApply.setOriginalId(smartApply.getAppid());
        }

        int result = smartSchoolService.insertSmartSchool(smartApply);

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

    /**
     * 更新学校基本信息
     * @param sa 学校基本信息数据
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult updateSmartSchoolById(UpdateSmartSchoolRequest sa, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

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

        SmartSchool oldData = smartSchoolService.getSmartById(sa.getId());
        if(oldData == null){
            return CommonResult.fail("学校基本信息无效，修改失败");
        }

        oldData.setSchoolCode(sa.getSchoolCode() == null ? oldData.getSchoolCode() : sa.getSchoolCode());
        oldData.setName(sa.getName() == null ? oldData.getName() : sa.getName());
        oldData.setSchoolBadge(sa.getSchoolBadge() == null ? oldData.getSchoolBadge() : sa.getSchoolBadge());
        oldData.setLogoImage(sa.getSchoolBadge() == null ? oldData.getLogoImage() : sa.getLogoImage());
        oldData.setOfficialName(sa.getOfficialName() == null ? oldData.getOfficialName() : sa.getOfficialName());
        oldData.setOriginalId(sa.getAppid() == null ? oldData.getOriginalId() : (oldData.getAppid().equals(sa.getAppid()) ? oldData.getOriginalId() : oldData.getAppid()));
        oldData.setAppid(sa.getAppid() == null ? oldData.getAppid() : sa.getAppid());

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

    @Override
    public CommonResult querySmartSchool() {
        SmartSchool result = smartSchoolService.getSmartSchool();

        return CommonResult.ok(result);
    }

    /**
     * 学校基本信息分页数据查询
     * @param currentPage 当前页数
     * @param pageCount 一页数据条数
     * @param name 查询名称
     * @return
     */
    @Override
    public CommonResult queryPageSmartSchools(int currentPage, int pageCount, String name) {
        PageUtils<SmartSchool> result = smartSchoolService.queryPageSmartSchools(currentPage, pageCount, name);

        return CommonResult.ok(result);
    }

    @Override
    public CommonResult deleteSmartSchoolById(int id) {

        SmartSchool data = smartSchoolService.getSmartById(id);

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

        int result = smartSchoolService.deleteSmartSchoolById(id);

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

