package com.sifang.modules.sunshine.SystemSetting.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.dao.CompanyInformationDao;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmPlantForm;
import com.sifang.modules.sunshine.SystemSetting.service.CompanyInformationService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


import java.util.List;

/**
 * 公司资料设置---平台端维护  controller
 */
@Controller
@RequestMapping(value = "${adminPath}/companyInformationSetting")
public class CompanyInformationController extends BaseController{
    @Autowired
    private CompanyInformationService companyInformationService;

    @Autowired
    AddressService addressService;
    /**
     * 获取公司资料设置信息
     * @return
     */
    @RequestMapping(value = "baseInformation")
    public String baseInformation(HttpServletRequest request, Model model){
        User user = UserUtils.getUser();
        List<Record> province = addressService.getProvinces();//获取所有的省
        if(province.size()>0){
            model.addAttribute("province",province);
        }
        Record rd = companyInformationService.getCompanyAllData(user.getId());//获取列表数据
        if(rd !=null){
            model.addAttribute("data",rd);
            if(StringUtils.isNotBlank(rd.getStr("province"))){
                List<Record> city = AddressUtils.getCityByProName(rd.getStr("province"));
                model.addAttribute("city",city);
                List<Record> area = AddressUtils.getAreaByCityName(rd.getStr("city"));
                model.addAttribute("area",area);
            }
        }
        return "modules/yangguang/systemSetting/companyInformation";
    }


    /**
     * 公司收款设置中的保存
     * @param request
     * @return
     */
    @RequestMapping(value = "colectionSave")
    @ResponseBody
    public Object colectionSave(HttpServletRequest request){
        return companyInformationService.colectionSave(request);
    }

    /**
     *修改密码
     * @param request
     * @return
     */
    @RequestMapping(value = "updatePassword")
    @ResponseBody
    public Object updatePassword(HttpServletRequest request){
        return companyInformationService.updatePassword(request);
    }


    /**
     *修改资料保存
     * @param request
     * @return
     */
    @RequestMapping(value = "getSave")
    @ResponseBody
    public Object getSave(HttpServletRequest request){
        return companyInformationService.getSave(request);
    }
    /**
     * 平台设置收款二维码
     */
    @Autowired
    private CompanyInformationDao companyInformationDao;
    @RequestMapping(value="setBarCode")
   public String setBarCode(Model model){
        User user=UserUtils.getUser();
        CrmPlantForm crmPlantForm=companyInformationDao.getPlantForm();
        model.addAttribute("crmPlant",crmPlantForm);
       return "modules/yangguang/systemSetting/barCodeSetting";
    }

    @ResponseBody
    @RequestMapping(value="platform/saveImg")
    public String saveImg(HttpServletRequest request){
       String img=request.getParameter("img");
       String type=request.getParameter("type");
       return companyInformationService.saveImg(img,type);
    }
}
