package com.syjy.calculate.conotroller;

import com.syjy.calculate.entity.CalculationFormula;
import com.syjy.calculate.entity.PageRes;
import com.syjy.calculate.repository.CalculationFormulaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 公式管理
 *
 * @author zcl
 * @version 1.0
 * @since 2022/8/8 9:30
 */
@Controller
@RequestMapping("calculateView/jarCalculate")
public class JarCalculate {
    @Autowired
    CalculationFormulaRepository calculationFormulaRepository;

    /**
     * 查询所有公式
     * @return
     */
    @PostMapping("select")
    @ResponseBody
    public PageRes select(CalculationFormula calculationFormula,HttpServletRequest request) {
        int limit = calculationFormula.getLimit();
        int offset = calculationFormula.getOffset();
        String province = calculationFormula.getProvince();
        String type =  calculationFormula.getType();
        int count = calculationFormulaRepository.selectCount(province,type);
        //查询出所有满足条件的数据（当前页面）
        List<CalculationFormula> list= calculationFormulaRepository.select(limit,offset,province,type);
        //封装返回给前端的数据
        PageRes<CalculationFormula> result = new PageRes<CalculationFormula>();
        result.setRows(list);
        result.setTotal(count);
        return result;
    }

    /**
     * 更新公式
     * @param id
     * @return
     */
    @PostMapping("getOne")
    @ResponseBody
    public CalculationFormula getOne(@RequestParam int id) {
        CalculationFormula calculationFormula = calculationFormulaRepository.getOne(id);
        return calculationFormula;
    }

    /**
     * 更新公式
     * @param calculationFormula
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public String edit(@RequestBody CalculationFormula calculationFormula) {
        if(calculationFormulaRepository.update(calculationFormula)==0){
            return "修改失败";
        }
        return "修改成功";
    }

    /**
     * 新增公式
     * @param calculationFormula
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public String add(@RequestBody CalculationFormula calculationFormula) {
        if(calculationFormulaRepository.add(calculationFormula)==0){
            return "新增失败";
        }
        return "新增成功";
    }

    @PostMapping("delete")
    @ResponseBody
    public String delete(@RequestParam int id) {
        if(calculationFormulaRepository.delete(id)==0){
            return "删除失败";
        }
        return "删除成功";
    }
}
