package com.service.contract;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Contract;
import com.model.Org;
import com.model.User;
import com.service.OrgService;
import com.utils.CommonUtils;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ContractService {
    public static final ContractService me = new ContractService();

    public void addOrUpdate(Contract bean, User user){
        if(bean.getId() != null && bean.getId()>0){
            bean.set("updatetime",new Date()).set("updateuserid", user.getId()).update();
        }else{
            bean.setOrgid(user.getOrgid()).setStatus(0).setIsdelete(0).set("createtime",new Date()).set("operatorid", user.getId()).save();
        }
    }
    public void changeStatus(int id, int status){
        Db.update("update t_contract set status = ? where id=?",status, id);
    }
    public void delete(int id){
        Db.update("update t_contract set isdelete=1 where id=?",id);
    }
    public Record getById(int id){
        Record bean = Db.findById("t_contract",id);
        if(bean != null) {
            List<Record> filelist = Db.find("select * from t_contract where contid = ?", bean.getInt("id"));
            if(filelist != null && filelist.size()>0){
                for(Record file : filelist){
                    if("申购".equals(file.getStr("fieldtype"))){
                        bean.set("shengou",file.get("fileurl"));
                    }else if("中标".equals(file.getStr("fieldtype"))){
                        bean.set("zhongbiao",file.get("fileurl"));
                    }else if("采购".equals(file.getStr("fieldtype"))){
                        bean.set("caigou",file.get("fileurl"));
                    }else if("验收".equals(file.getStr("fieldtype"))){
                        bean.set("yanshou",file.get("fileurl"));
                    }
                }
            }
        }
        return bean;
    }
    public Page page(int pageSize, int pageNumber, int orgid,Map param){
        String select = "select c.*,o.orgname,o.orgtypeid ";
        StringBuilder where = new StringBuilder(" from t_contract c left join t_org o on o.id=c.orgid where c.isdelete=0 AND orgid=?");
        List objects = new ArrayList();
        objects.add(orgid);
        if(param.get("contname") != null && !"".equals(param.get("contname"))){
            where.append(" and c.contname like ?");
            objects.add("%"+param.get("contname")+"%");
        }
        if(param.get("contcode") != null && !"".equals(param.get("contcode"))){
            where.append(" and c.contcode =?");
            objects.add(param.get("contcode"));
        }
        if(param.get("status") != null && !"".equals(param.get("status"))){
            where.append(" and c.status = ?");
            objects.add(param.get("status"));
        }
        if(param.get("startdate") != null && !"".equals(param.get("startdate"))){
            where.append(" and c.signedtime >=?");
            objects.add(param.get("startdate"));
        }
        if(param.get("enddate") != null && !"".equals(param.get("enddate"))){
            where.append(" and c.signedtime <=?");
            objects.add(param.get("enddate"));
        }
        where.append(" order by c.createtime desc");
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(objects));
        List<Record> list = page.getList();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if(list != null && list.size()>0){
            for(Record record : list){
                if(record.get("signedtime") != null) {
                    record.set("signedtime",format.format(record.get("signedtime")));
                }
                if(record.get("checktime") != null) {
                    record.set("checktime",format.format(record.get("checktime")));
                }
            }
        }
        return page;
    }
    public Page<Record> checkpage(int pageSize, int pageNumber, int orgid, String schoolids,Map param){

        String select = "select c.*,o.orgname,o.id as orgid ";
        StringBuilder where = new StringBuilder(" from t_contract c left join t_org o on o.id=c.orgid where c.isdelete = 0 ");
        List objects = new ArrayList();
        if(param.get("contname") != null && !"".equals(param.get("contname"))){
            where.append(" and c.contname like ?");
            objects.add("%"+param.get("contname")+"%");
        }
        if(param.get("contcode") != null && !"".equals(param.get("contcode"))){
            where.append(" and c.contcode =?");
            objects.add(param.get("contcode"));
        }
        if(param.get("status") != null && !"".equals(param.get("status"))){
            where.append(" and c.status = ?");
            objects.add(param.get("status"));
        }
        if(param.get("startdate") != null && !"".equals(param.get("startdate"))){
            where.append(" and c.signedtime >=?");
            objects.add(param.get("startdate"));
        }
        if(param.get("enddate") != null && !"".equals(param.get("enddate"))){
            where.append(" and c.signedtime <=?");
            objects.add(param.get("enddate"));
        }
//        Record org = Db.findById("t_org",orgid);
//        where.append(" and o.parentcodes like ? and ((c.status=1 and o.parentcodes=?) or c.status=2) order by c.createtime desc");
//        objects.add(orgid+"%");
//        objects.add(org.getStr("parentcodes")+org.getInt("id"));

        //当前级和所有下级
        Record org = Db.findById("t_org",orgid);
        String parentcodes = "";
        if(org.getInt("orgtypeid") != 1){
            parentcodes = org.getStr("parentcodes")+CommonUtils.codeAddOne(orgid,6);
        }else{
            parentcodes = CommonUtils.codeAddOne(orgid,6);
        }
        where.append(" and ((parentcodes=? and c.status=1) or (parentcodes like ? and c.status=2) ) order by c.createtime desc ");

        objects.add(parentcodes);
        objects.add(parentcodes+"%");
        Page<Record> page =  Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(objects));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for(Record r : page.getList()){
            //Integer rorgid = r.getInt("orgid");
            /*Record rd = Db.findFirst("SELECT parentid FROM t_org WHERE id = ?",rorgid);
            Integer parentid = rd.getInt("parentid");
            if(orgid == parentid){
                r.set("isshow",0);
            }else {
                r.set("isshow",1);
            }*/
            if(r.get("signedtime") != null) {
                r.set("signedtime",format.format(r.get("signedtime")));
            }
            if(r.get("checktime") != null) {
                r.set("checktime",format.format(r.get("checktime")));
            }
        }
        return page;
    }
}
