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.CustomerAddressMapper;
import com.th.demo.mapping.maint.CustomerMapper;
import com.th.demo.mapping.system.SysUserMapper;
import com.th.demo.model.maint.Customer;
import com.th.demo.model.maint.CustomerAddress;
import com.th.demo.model.system.ResponseCodeMsg;
import com.th.demo.model.system.SysUser;
import com.th.demo.model.system.Type;
import com.th.demo.service.maint.CustomerService;
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 org.springframework.transaction.annotation.Transactional;

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

@Service("CustomerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    public CustomerMapper customerMapper;
    @Autowired
    public CustomerAddressMapper customerAddressMapper;

    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<Customer> list = new ArrayList<Customer>();

        list = customerMapper.select(query, belongId);

        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        result = JSONTools.toString(pageInfo);
        return result;
    }

    @Override
    public String save(String json, String userId, String belongId) {
        result = Type.FAIL;
        Customer customer = JSON.parseObject(json, new TypeReference<Customer>() {
        });

        if (customer.getId() == null || customer.getId().equals("")) {
            // 防止维护重复，增加信息校验
            List<Customer> customerList = customerMapper.selectByNameCode(customer.getName(),customer.getCode(),belongId);
            if (customerList.size() >= 1){
                return JSONTools.toString(1,"该客户名称或客户简称已存在，请勿重复维护");
            }
            customer.setAddId(userId);
            customer.setAddTime(new Date());
            customer.setModifyId(userId);
            customer.setModifyTime(new Date());
            customer.setCancelFlag("0");
            customer.setBelongId(belongId);
            num = customerMapper.insert(customer);
        } else {
            List<Customer> customerList = customerMapper.selectByNameCode(customer.getName(),customer.getCode(),belongId);
            if (customerList.size() == 1 && !customer.getId().equals(customerList.get(0).getId())){
                return JSONTools.toString(1,"该客户名称或客户简称已存在，请勿重复维护");
            } else if (customerList.size() > 1) {
                return JSONTools.toString(1,"该客户名称或客户简称已存在，请勿重复维护");
            }
            customer.setModifyId(userId);
            customer.setModifyTime(new Date());
            num = customerMapper.updateByPrimaryKey(customer);
        }
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }


    @Override
    public String remove(String id, String userId) {
        Customer customer = customerMapper.selectByPrimaryKey(id);
        customer.setCancelFlag("9");
        customer.setCancelId(userId);
        customer.setCancelTime(new Date());
        num = customerMapper.updateByPrimaryKey(customer);
        result = Tools.moreThanZeroResultJSON(num);
        return result;

    }

    @Override
    public String queryAddress(String fId, String userId, String belongId) {
        List<CustomerAddress> list = customerAddressMapper.selectByFid(fId, belongId);
        result = JSONTools.toString(list);
        return result;
    }

    @Transactional
    @Override
    public String saveAddress(String json, String userId, String belongId) {
        result = Type.FAIL;
        CustomerAddress customerAddress = JSON.parseObject(json, new TypeReference<CustomerAddress>() {
        });
        List<CustomerAddress> list = customerAddressMapper.selectByFid(customerAddress.getfId(), belongId);
        if (list == null || list.size() == 0) {
            customerAddress.setDefaultFlag("1");
            Customer customer = customerMapper.selectByPrimaryKey(customerAddress.getfId());
            customer.setAddress(customerAddress.getAddress());
            customerMapper.updateByPrimaryKey(customer);
        } else {
            customerAddress.setDefaultFlag("0");
        }
        customerAddress.setAddId(userId);
        customerAddress.setAddTime(new Date());
        customerAddress.setCancelFlag("0");
        customerAddress.setBelongId(belongId);
        num += customerAddressMapper.insert(customerAddress);
        result = Tools.moreThanZeroResultJSON(num);
        return result;
    }
@Transactional
    @Override
    public String defaultAddress(String json, String userId, String belongId) {

        CustomerAddress customerAddress = JSON.parseObject(json, new TypeReference<CustomerAddress>() {
        });
        Customer customer = customerMapper.selectByPrimaryKey(customerAddress.getfId());
        customer.setAddress(customerAddress.getAddress());
        customerMapper.updateByPrimaryKey(customer);
        List<CustomerAddress> list = customerAddressMapper.selectByFid(customerAddress.getfId(), belongId);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getDefaultFlag().equals("1")) {
                list.get(i).setDefaultFlag("0");
                num += customerAddressMapper.updateByPrimaryKey(list.get(i));
            }
        }
        customerAddress.setDefaultFlag("1");
        num += customerAddressMapper.updateByPrimaryKey(customerAddress);
        return Tools.moreThanZeroResultJSON(num);
    }

    @Transactional
    @Override
    public String queryByUserId(String userId, String belongId) {

        List<Customer> list = customerMapper.selectByUserId(userId, belongId);
        return JSONTools.toString(list);
    }
}
