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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.AreaMapper;
import com.th.demo.mapping.maint.CargoMapper;
import com.th.demo.mapping.maint.CustomerMapper;
import com.th.demo.mapping.ware.CargoChangeRecordMapper;
import com.th.demo.model.maint.Customer;
import com.th.demo.model.maint.Area;
import com.th.demo.model.maint.Cargo;
import com.th.demo.model.maint.Ware;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.model.ware.CargoChangeRecord;
import com.th.demo.service.maint.CargoService;
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("CargoService")
public class CargoServiceImpl implements CargoService {
    @Autowired
    public CargoMapper cargoMapper;
    @Autowired
    public CustomerMapper customerMapper;
    @Autowired
    public CargoChangeRecordMapper cargoChangeRecordMapper;

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


    @Override
    public String query(int page, int rows, String uperCustomer, String Customer ,String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<Cargo> list   = cargoMapper.selectAll(uperCustomer, Customer, belongId);
        PageInfo<Cargo> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        result = Type.FAIL;
        JSONObject object = JSON.parseObject(json);
        String uperCustomer = object.get("uperCustomer").toString();
        String downCustomer = object.get("Customer").toString();
        Cargo cargo = cargoMapper.selectByByFkAndCustomer(uperCustomer, downCustomer, belongId);
        Customer uperCust = customerMapper.selectByPrimaryKey(uperCustomer);
        Customer downCust = customerMapper.selectByPrimaryKey(downCustomer);
        String subChange = object.get("sub").toString();
        if (object.get("weight").toString().equals("")){
            return JSONTools.toString(1,"请确认重量信息");
        }
        Double weight = Double.parseDouble(object.get("weight").toString());
        if (cargo == null){
            if (subChange.equals("down")){
                return JSONTools.toString(1,"新建信息请选择增加重量");
            } else if (subChange.equals("")) {
                return JSONTools.toString(1,"请选择修改类型");
            }

            cargo = new Cargo();
            cargo.setUperCustomer(uperCust);
            cargo.setCustomer(downCust);
            cargo.setSumWeight(weight);
            cargo.setBelongId(belongId);

            num= cargoMapper.insert(cargo);

            result = Tools.moreThanZeroResultJSON(num);

        }
        else{
            if (subChange.equals("down")){
                if (weight > cargo.getSumWeight()){
                    return JSONTools.toString(1,"需要减少的重量不足，请确认后再进行操作");
                }
                cargo.setSumWeight(cargo.getSumWeight() - weight);
            }else if (subChange.equals("up")){
                cargo.setSumWeight(cargo.getSumWeight() + weight);
            }else if (subChange.equals("")) {
                return JSONTools.toString(1,"请选择修改类型");
            }

            // cargo.setBelongId(belongId);
            num = cargoMapper.updateByPrimaryKey(cargo);
            result = Tools.moreThanZeroResultJSON(num);
        }
        CargoChangeRecord cargoChangeRecord = new CargoChangeRecord();
        cargoChangeRecord.setUperCustomer(uperCust);
        cargoChangeRecord.setCustomer(downCust);
        cargoChangeRecord.setChangeFrom("手动调整");
        cargoChangeRecord.setWeight(weight);
        cargoChangeRecord.setOrdNo(object.get("ordNo").toString());
        cargoChangeRecord.setRemark(object.get("remark").toString());
        if (subChange.equals("up")){
            cargoChangeRecord.setSubStr("增加");
        }
        if (subChange.equals("down")){
            cargoChangeRecord.setSubStr("减少");
        }

        cargoChangeRecord.setModifyTime(new Date());
        cargoChangeRecord.setModifyUser(new SysUser(userId));
        num = cargoChangeRecordMapper.insert(cargoChangeRecord);
        result = Tools.moreThanZeroResultJSON(num);

        return result;
    }

    @Override
    public String remove(String id, String userId, String belongId) {
        num = cargoMapper.deleteByPrimaryKey(id);
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }

    @Override
    public String queryRecord(int page, int rows, String uperCustomer, String Customer ,
                              Date RecordDateBegin,Date RecordDateEnd,String subStr,String changeFrom,
                              String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<CargoChangeRecord> list   = cargoChangeRecordMapper.selectRecord(uperCustomer, Customer,RecordDateBegin,RecordDateEnd,subStr,changeFrom);
        PageInfo<CargoChangeRecord> pageInfo = new PageInfo<>(list);
        result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        return result;
    }
}
