package com.th.demo.service.impl.maint;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.AreaMapper;
import com.th.demo.mapping.maint.OrdTractMapper;
import com.th.demo.model.maint.Area;
import com.th.demo.model.maint.OrdTract;
import com.th.demo.model.maint.Price;
import com.th.demo.model.maint.Ware;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.AreaService;
import com.th.demo.service.maint.OrdTractService;
import com.th.demo.tools.JSONTools;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("OrdTractService")
public class OrdTractServiceImpl implements OrdTractService {

    @Autowired
    public OrdTractMapper ordTractMapper;

    String result = Type.FAIL;
    int num = 0;

    @Override
    public String query(int page, int rows, String query, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<OrdTract> list   = ordTractMapper.select(query, belongId);
        PageInfo<OrdTract> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        result = Type.FAIL;
        OrdTract ordTract = JSON.parseObject(json,new TypeReference<OrdTract>(){});
        ordTract.setCancelFlag("0");

        List<OrdTract> ordTractList = ordTractMapper.selectByOrdNo(ordTract.getOrdNo(),belongId);
        if (ordTractList.size() == 0){
            num = ordTractMapper.insert(ordTract);
        }else {
            num = ordTractMapper.update(ordTract);
        }
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }

    @Override
    public String inImport(String json, String userId, String belongId){
        List<JSONObject> listJO = JSON.parseObject(json, new TypeReference<List<JSONObject>>() {
        });
        OrdTract ordTract = new OrdTract();
        for (int i = 0; i < listJO.size(); i++) {
            ordTract = new OrdTract();
            if(listJO.get(i).get("合约号") != null && !listJO.get(i).get("合约号").toString().equals("")){
                ordTract.setContractNo(listJO.get(i).get("合约号").toString());
            }
            if(listJO.get(i).get("订单号") != null && !listJO.get(i).get("订单号").toString().equals("")){
                ordTract.setOrdNo(listJO.get(i).get("订单号").toString());
            }else {
                return JSONTools.toString(1,"订单号必须维护");
            }
            if(listJO.get(i).get("支付方式") != null && !listJO.get(i).get("支付方式").toString().equals("")){
                ordTract.setPayWay(listJO.get(i).get("支付方式").toString());
            }else {
                return JSONTools.toString(1,"支付方式必须维护");
            }
            if (!ordTract.getPayWay().equals("现金") && !ordTract.getPayWay().equals("月付")){
                return JSONTools.toString(1,"支付方式维护了“现金”和“月付”两种");
            }
            if(listJO.get(i).get("备注信息") != null && !listJO.get(i).get("备注信息").toString().equals("")){
                ordTract.setRemark(listJO.get(i).get("备注信息").toString());
            }
            num += ordTractMapper.insert(ordTract);

        }
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }

    @Override
    public String remove(String ordNo, String userId,String belongid){
        List<OrdTract> ordTractList = ordTractMapper.selectByOrdNo(ordNo,belongid);
        if (ordTractList.size() > 0){
            ordTractList.get(0).setCancelFlag("1");
            num = ordTractMapper.update(ordTractList.get(0));
            result = Tools.moreThanZeroResultJSON(num);
        }

        return result;
    }



}
