package com.ag.dynamicmodule.basecode.controller;

import com.ag.dynamicmodule.basecode.dao.CClientInvoiceDao;
import com.ag.dynamicmodule.basecode.dao.CClientTypeRelaDataDao;
import com.ag.dynamicmodule.basecode.pojo.CClientInvoice;
import com.ag.util.CommonUtil;
import com.alibaba.fastjson.JSON;
import com.wb.util.WebUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2019/9/19 0019.
 */
@Controller
public class CClientInvoiceController {
    @Autowired
    CClientInvoiceDao cClientInvoiceDao;
    @Autowired
    CClientTypeRelaDataDao cClientTypeRelaDataDao;
    /**
     * 保存数据
     * @param object
     * @param response
     * @param request
     */
    public  void  saveMethod(JSONObject object, HttpServletResponse response, HttpServletRequest request){
        CClientInvoice cClientInvoice = JSON.parseObject(object.getString("cClientInvoiceData"),CClientInvoice.class);
            String username = (String) request.getSession().getAttribute("sys.username");
            cClientInvoice.setCreatedBy(username);
            cClientInvoice.setCreatedOn(CommonUtil.getNowDate());
            cClientInvoice.setUpdatedBy(username);
            cClientInvoice.setUpdatedOn(CommonUtil.getNowDate());
            cClientInvoiceDao.insert(cClientInvoice);
            String datajson = JSON.toJSONString(cClientInvoice);
        try {
            WebUtil.send(response, CommonUtil.upJson("["+datajson+"]"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void  saveUPMethod(JSONObject object, HttpServletResponse response, HttpServletRequest request){
        CClientInvoice cClientInvoice = JSON.parseObject(object.getString("cClientInvoiceData"),CClientInvoice.class);
            String username = (String) request.getSession().getAttribute("sys.username");
            cClientInvoice.setUpdatedBy(username);
            cClientInvoice.setUpdatedOn(CommonUtil.getNowDate());
            cClientInvoiceDao.update(cClientInvoice);
        String datajson = JSON.toJSONString(cClientInvoice);
        try {
            WebUtil.send(response, CommonUtil.upJson("["+datajson+"]"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除记录
     * @param object
     * @param response
     * @param request
     */
    public void delCClient(JSONObject object, HttpServletResponse response, HttpServletRequest request) {
        String ids = request.getParameter("ids");
        cClientInvoiceDao.delete(ids);
    }

    public void delCClientType(JSONObject object, HttpServletResponse response, HttpServletRequest request) {
        String ids = request.getParameter("ids");
        cClientTypeRelaDataDao.deleteByClientCode(ids);
    }
}
