TaskDao.java 5.21 KB
package vion.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import vion.model.FaultLog;
import vion.model.Task;

import java.util.Date;
import java.util.List;

@Repository
public class TaskDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String SELECT_WORKORDRE_ALL = "SELECT * FROM tbl_task_info";

    public List<Task> getTaskAll() {
        return jdbcTemplate.query(SELECT_WORKORDRE_ALL, new BeanPropertyRowMapper<Task>(Task.class));
    }

    private static final String SELECT_WORKORDRE_BY_ID = "SELECT * FROM tbl_task_info where id=?";

    public Task getTaskByID(Integer id) {
        List<Task> tasks= jdbcTemplate.query(SELECT_WORKORDRE_BY_ID,new Object[]{id},
                new BeanPropertyRowMapper<Task>(Task.class));
        if(tasks.size()>0){
            return tasks.get(0);
        }else{
            return null;
        }
    }


    private static final String SELECT_WORKORDRE_BY_PARM= "SELECT * FROM tbl_task_info WHERE account_id=?";

    public List<Task> getTaskByParm(Integer accountid,Integer store_id, Date startdate,
                                              Date enddate,Integer status,Integer fault_type) {

        StringBuffer getTaskSb = new StringBuffer(SELECT_WORKORDRE_BY_PARM);


        if(store_id!=null){
            getTaskSb.append(" and store_id = "+store_id+"");
        }
        if(startdate!=null){
            getTaskSb.append(" and repair_date>='"+startdate+"'");
        }
        if(enddate!=null){
            getTaskSb.append(" and repair_date<='"+enddate+"'");
        }
        if(status!=null){
            getTaskSb.append(" and status="+status+"");
        }
        if(fault_type!=null){
            getTaskSb.append(" and fault_type="+fault_type+"");
        }

        return jdbcTemplate.query(getTaskSb.toString(),new Object[]{accountid},
                new BeanPropertyRowMapper<Task>(Task.class));
    }

    private static final String SAVE_WORKORDRE= "INSERT INTO tbl_task_info(\n" +
            "            store_id, repair_date, fault_type, fault_description, repair_people, \n" +
            "            repair_phone, status, solve_date, fault_reason, solve_type, solve_description, \n" +
            "            create_user, remark,account_id)\n" +
            "    VALUES (?, ?, ?, ?, ?, ?, \n" +
            "            ?, ?, ?, ?, ?, ?, \n" +
            "            ?, ? );";
/*
    public Task addTask(Task task) {
        return jdbcTemplate.queryForObject(SAVE_WORKORDRE,
                new Object[]{
                        task.getStore_id(),task.getRepair_date(),task.getFault_type(),
                        task.getFault_description(),task.getRepair_people(),task.getRepair_phone(),
                        task.getStatus(),task.getSolve_date(),task.getFault_reason(),task.getSolve_type(),
                        task.getSolve_description(),task.getCreate_user(),
                        task.getRemark(),task.getAccount_id()}
                , new BeanPropertyRowMapper<Task>(Task.class));
    }

    private static final String UPDATE_WORKORDRE= "UPDATE tbl_task_info\n" +
            "   SET store_id=?, repair_date=?, fault_type=?, fault_description=?, \n" +
            "       repair_people=?, repair_phone=?, status=?, solve_date=?, fault_reason=?, \n" +
            "       solve_type=?, solve_description=?, create_user=?, \"current_user\"=?, \n" +
            "       modify_time=?, remark=?, account_id=?\n" +
            " WHERE id=?;";

    public Integer updateTask(Task task) {
        return jdbcTemplate.update(UPDATE_WORKORDRE,
                new Object[]{
                        task.getStore_id(),task.getRepair_date(),task.getFault_type(),
                        task.getFault_description(),task.getRepair_people(),task.getRepair_phone(),
                        task.getStatus(),task.getSolve_date(),task.getFault_reason(),task.getSolve_type(),
                        task.getSolve_description(),task.getCreate_user(),task.getCurrent_user(),
                        task.getModify_time(),task.getRemark(),task.getAccount_id(),task.getId()});
    }


    private static final String SELECT_FAULTLOG_BY_STOREID = "SELECT * FROM tbl_fault_log where store_id=? and workorder_id=?";

    public FaultLog getFaultlogByStoreID(Integer store_id,Integer workorder_id) {
        List<FaultLog> faultLogs= jdbcTemplate.query(SELECT_FAULTLOG_BY_STOREID,new Object[]{store_id,workorder_id},
                new BeanPropertyRowMapper<FaultLog>(FaultLog.class));
        if(faultLogs.size()>0){
            return faultLogs.get(0);
        }else{
            return null;
        }
    }


    private static final String SAVE_FAULTLOG= "INSERT INTO tbl_fault_log(\n" +
            "            store_id, workorder_id, operator, content, remark)\n" +
            "    VALUES (?, ?, ?, ?, ?);";

    public Integer addFaultLog(FaultLog faultLog) {
        return jdbcTemplate.update(SAVE_FAULTLOG,
                new Object[]{
                        faultLog.getStore_id(),faultLog.getTask_id(),faultLog.getOperator(),
                        faultLog.getContent(),faultLog.getRemark()});
    }*/
}