package co.dc.aptiv.service;

import co.dc.aptiv.AptivApp;
import co.dc.commons.basedao.BaseDao;
import co.dc.aptiv.ScanFrame;
import co.dc.aptiv.pojo.BillDetailPojo;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @describe: 单据详情
 * @author: liuxinglong
 * @version: 2018/4/17 15:21
 */
@Service("billDetailService")
public class BillDetailService extends BaseDao<BillDetailPojo> {

    private static final Log log = LogFactory.getLog(BillDetailService.class);
    @Resource
    private BillService billService;
    @Resource
    private PropertiesConfiguration configuration;

    private ScanFrame scanFrame;

    public void setScanFrame(ScanFrame scanFrame) {
        this.scanFrame = scanFrame;
    }

    @Override
    protected String getTableName() {
        return BillDetailPojo.tableName;
    }

    /**
     * 根据pcbillid查询产品列表
     *
     * @param pcbillid
     * @return
     */
    public List<BillDetailPojo> getinfoGoods(long pcbillid) {
        log.info("根据pcbillid查询产品列表-long pcbillid=" + pcbillid);
        List<BillDetailPojo> billDetailPojos = new ArrayList<BillDetailPojo>();
        try {
            billDetailPojos = queryRunner.query("select * from " + getTableName() + " where pcbillid = " + pcbillid + " and delflag = 0 order by modifyTime desc",
                    new BeanListHandler<BillDetailPojo>(BillDetailPojo.class));
        } catch (SQLException e) {
            log.error("根据pcbillid查询产品列表-异常:", e);
        }
        return billDetailPojos;
    }

    /**
     * 查询所有为上传的产品列表并且变更上传字段的值
     *
     * @return
     */
    public List<BillDetailPojo> getunupedGoods() {
        log.info("查询所有为上传的产品列表");
        List<BillDetailPojo> billDetailPojos = new ArrayList<BillDetailPojo>();
        Map<String, Object> where = new HashMap<String, Object>();
        where.put("updatedflag", 0);
        billDetailPojos = listByParam(where);
        if (billDetailPojos != null && billDetailPojos.size() > 0) {
            update("updatedflag", 1, "updatedflag", 0);
        }
        return billDetailPojos;
    }


    /**
     * 获取物料在详情表中最后一条记录的创建日期
     *
     * @param materielId 物料Id
     * @return
     */
    public String getLastDate(long materielId) {
        try {
            String sql = "SELECT * FROM bill_detail WHERE delFlag = 0 AND materielId = " + materielId + " ORDER BY createTime DESC LIMIT 1";
            BillDetailPojo billDetailPojo = queryRunner.query(sql, new BeanHandler<BillDetailPojo>(BillDetailPojo.class));
            if (billDetailPojo != null) {
                return billDetailPojo.getCreateTime().substring(0, 10);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 保存单据详情
     *
     * @param billId         单据Id
     * @param materielId     物料Id
     * @param qrCode         生成的二维码
     * @param colledtionCode 采集器返回信息
     * @return
     */
    public BillDetailPojo addBillDetail(Long billId, Long materielId, String qrCode, String colledtionCode) {
        BillDetailPojo billDetailPojo = new BillDetailPojo();
        billDetailPojo.setBillId(billId);
        billDetailPojo.setMaterielId(materielId);
        billDetailPojo.setQrCode(qrCode);
        billDetailPojo.setCollectionCode(colledtionCode);
        billDetailPojo.setState(qrCode.equals(colledtionCode) ? 1 : 0);
        billDetailPojo.setCreateTime(AptivApp.qrcodeService.createDateTime);
        billDetailPojo.setModifyTime(AptivApp.qrcodeService.createDateTime);
        billDetailPojo.setDelFlag(0);
        Long billDetailId = this.save(billDetailPojo);
        billDetailPojo.setId(billDetailId);
        return billDetailPojo;
    }


    public void cllj() {
        //拿到当前单据信息
        //拿到当前发出去的码
        //拿到当前收到的码

    }

}
