th.co.geniustree.dental.controller.DetailHealController.java Source code

Java tutorial

Introduction

Here is the source code for th.co.geniustree.dental.controller.DetailHealController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package th.co.geniustree.dental.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import th.co.geniustree.dental.model.DetailHeal;
import th.co.geniustree.dental.model.SearchData;
import th.co.geniustree.dental.repo.DetailHealRepo;
import th.co.geniustree.dental.repo.OrderBillRepo;
import th.co.geniustree.dental.repo.OrderHealRepo;
import th.co.geniustree.dental.service.DetailHealService;
import th.co.geniustree.dental.spec.DetailHealSpec;

/**
 *
 * @author Jasin007
 */
@RestController
public class DetailHealController {

    @Autowired
    private DetailHealRepo detailHealRepo;
    @Autowired
    private DetailHealService detailHealService;
    @Autowired
    private OrderHealRepo orderHealRepo;

    @RequestMapping(value = "/savedetailheal", method = RequestMethod.POST)
    public void saveDetailHeal(@RequestBody DetailHeal detailHeal) {
        detailHealRepo.save(detailHeal);
    }

    @RequestMapping(value = "/loaddetailheal")
    public Page<DetailHeal> loadOrderHeal(Pageable pageable) {
        return detailHealRepo.findAll(pageable);
    }

    @RequestMapping(value = "/loaddetailhealforbill")
    public Page<DetailHeal> loadOrderHealForBill(Pageable pageable) {
        return detailHealRepo.findByStatusIsNull(pageable);
    }

    @RequestMapping(value = "/deletedetailheal", method = RequestMethod.POST)
    public void deleteDetailHeal(@RequestBody DetailHeal detailHeal) {
        detailHealRepo.delete(detailHeal.getId());
    }

    @RequestMapping(value = "/totaldetailheal", method = RequestMethod.GET)
    public Long getTotalDetailHeal() {
        return detailHealRepo.count();
    }

    @RequestMapping(value = "/totaldetailhealforbill", method = RequestMethod.GET)
    public Integer getTotalDetailHealForBill() {
        return detailHealRepo.findByStatusIsNull().size();
    }

    @RequestMapping(value = "/loaddetailheal/searchdetailheal", method = RequestMethod.POST)
    public Page<DetailHeal> search(@RequestBody SearchData searchData, Pageable pageable) throws ParseException {
        String keyword = searchData.getKeyword();
        String searchBy = searchData.getSearchBy();
        Page<DetailHeal> detailHeals = null;
        if ("".equals(searchBy)) {
            detailHeals = detailHealService.searchByPatient(keyword, pageable);
        }
        if ("".equals(searchBy)) {
            detailHeals = detailHealService.searchByDoctor(keyword, pageable);
        }
        if ("?".equals(searchBy)) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            Date keywordDate = df.parse(keyword);
            detailHeals = detailHealService.searchByDateHeal(keywordDate, pageable);
        }
        return detailHeals;
    }

    @RequestMapping(value = "/loaddetailheal/searchdetailhealforbill", method = RequestMethod.POST)
    public Page<DetailHeal> searchForBill(@RequestBody SearchData searchData, Pageable pageable)
            throws ParseException {
        String keyword = searchData.getKeyword();
        String searchBy = searchData.getSearchBy();
        Page<DetailHeal> detailHeals = null;
        if ("".equals(searchBy)) {
            detailHeals = detailHealService.searchByPatientForBill(keyword, pageable);
        }
        if ("".equals(searchBy)) {
            detailHeals = detailHealService.searchByDoctorForBill(keyword, pageable);
        }
        if ("?".equals(searchBy)) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            Date keywordDate = df.parse(keyword);
            detailHeals = detailHealService.searchByDateHealForBill(keywordDate, pageable);
        }
        return detailHeals;
    }

    @RequestMapping(value = "/countdetailheal", method = RequestMethod.GET)
    public long countDetailHeal() {
        return detailHealRepo.count();
    }

    @RequestMapping(value = "/countdetailhealforbill", method = RequestMethod.GET)
    public long countDetailHealForBill() {
        return detailHealRepo.findByStatusIsNull().size();
    }

    @RequestMapping(value = "/countsearchdetailheal", method = RequestMethod.POST)
    public long countSearchDetailHeal(@RequestBody SearchData searchData) throws ParseException {
        long count = 0;
        String keyword = searchData.getKeyword();
        String searchBy = searchData.getSearchBy();
        Page<DetailHeal> detailHeals = null;
        if ("".equals(searchBy)) {
            count = detailHealRepo.count(DetailHealSpec.patientLike("%" + keyword + "%"));
        }
        if ("".equals(searchBy)) {
            count = detailHealRepo.count(DetailHealSpec.doctorLike("%" + keyword + "%"));
        }
        if ("Date?Heal".equals(searchBy)) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            Date keywordDate = df.parse(keyword);
            count = detailHealRepo.count(DetailHealSpec.dateHealLike(keywordDate));
        }
        return count;
    }

    @RequestMapping(value = "/countsearchdetailhealforbill", method = RequestMethod.POST)
    public long countSearchDetailHealForBill(@RequestBody SearchData searchData) throws ParseException {
        long count = 0;
        String keyword = searchData.getKeyword();
        String searchBy = searchData.getSearchBy();
        Page<DetailHeal> detailHeals = null;
        if ("".equals(searchBy)) {
            count = detailHealRepo.count(DetailHealSpec.patientLikeForBill("%" + keyword + "%"));
        }
        if ("".equals(searchBy)) {
            count = detailHealRepo.count(DetailHealSpec.doctorLikeForBill("%" + keyword + "%"));
        }
        if ("Date?Heal".equals(searchBy)) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            Date keywordDate = df.parse(keyword);
            count = detailHealRepo.count(DetailHealSpec.dateHealLikeForBill(keywordDate));
        }
        return count;
    }

    @RequestMapping(value = "/removeorderheal", method = RequestMethod.POST)
    private void removeOrderHeal(@RequestBody DetailHeal detailHeal) {
        for (int i = 0; i < detailHeal.getOrderHealDetailHeals().size(); i++) {
            orderHealRepo.delete(detailHeal.getOrderHealDetailHeals().get(i));
        }
    }
}