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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.demo.mapping.maint.PriceMapper;
import com.th.demo.mapping.maint.ProdPriceMapper;
import com.th.demo.mapping.maint.CustomerMapper;
import com.th.demo.model.maint.Price;
import com.th.demo.model.maint.ProdPrice;
import com.th.demo.model.maint.Customer;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.PriceService;
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("PriceService")
public class PriceServiceImpl implements PriceService {

    @Autowired
    public PriceMapper priceMapper;
    @Autowired
    ProdPriceMapper prodPriceMapper;
    @Autowired
    public CustomerMapper customerMapper;

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

    @Override
    public String query(int page, int rows, String materialName,String standard, String userId, String belongId) {
        PageHelper.startPage(page, rows);
        List<Price> list = priceMapper.select(materialName,belongId);
        PageInfo<Price> pageInfo = new PageInfo<>(list);
        result = JSONTools.toStringyyyyMMddHHmmss(pageInfo);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        Price price = JSON.parseObject(json,new TypeReference<Price>(){});
        if(price.getId()==null||price.getId().equals("")){
            Customer customers = customerMapper.selectByName(price.getCustomer().getName(),belongId);
            if (customers.getId()==null||customers.getId().equals("")){
                result = JSONTools.toStringyyyyMMddHHmmss(0);
                result = "该客户名称不存才";
                return result;
            }
            int priceCnt = priceMapper.selectCntByMaterialChangeTime(customers.getId(),0 , belongId);
            if(priceCnt > 0){
                result = JSONTools.toStringyyyyMMddHHmmss(0);
                result = "已存在该价格信息";

                return result;
            }
            price.setAddUser(new SysUser(userId));
            price.setAddTime(new Date());
            price.setModifyUser(new SysUser(userId));
            price.setModifyTime(new Date());
            price.setCancelFlag("0");
            price.setBelongId(belongId);
            num += priceMapper.insert(price);
        }else {
            price.setModifyUser(new SysUser(userId));
            price.setModifyTime(new Date());
            num += priceMapper.updateByPrimaryKey(price);
        }


        return  Tools.moreThanZeroResultJSON(num);

    }

    @Override
    public String remove(String id, String userId, String belongId) {
        Price price = priceMapper.selectByPrimaryKey(id);
        price.setCancelId(userId);
        price.setCancelFlag("1");
        price.setCancelTime(new Date());
        num += priceMapper.updateByPrimaryKey(price);
        return  Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String queryPriceNew(String userId, String belongId) {
        List<ProdPrice> list = prodPriceMapper.queryPriceNew();
        result = JSONTools.toStringyyyyMMddHHmmss(list);
        return result;
    }

    @Override
    public String deletePriceNew(String id, String userId, String belongId) {
        num = prodPriceMapper.deletePriceNew(id,userId);
        return  Tools.moreThanZeroResultJSON(num);
    }

    @Override
    public String savePriceNew(String json, String userId, String belongId) {
        ProdPrice prodPrice = JSON.parseObject(json,new TypeReference<ProdPrice>(){});
        prodPrice.setCrtId(userId);
        ProdPrice exit = prodPriceMapper.selectByProdNm(prodPrice.getProdNm());
        if(exit != null){
            return JSONTools.toString(1,"该物料种类已存在价格，请不要重复维护");
        }
        num = prodPriceMapper.savePriceNew(prodPrice);
        return Tools.moreThanZeroResultJSON(num);
    }

}
